Laravel:将上传的文件保存到Session中 [英] Laravel: Save uploaded file into Session

查看:173
本文介绍了Laravel:将上传的文件保存到Session中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宁愿将文件保存到Session中并在以后的条件下上传,而不是直接将文件上传和移动到服务器上的某个位置.

Instead of uploading and moving the file directly to a place on the server, I would rather save it into the Session and upload it on a condition at a later point.

这是我当前将文件保存到服务器的方法:

Here is my Method that currently saves the File to my server:

public function step3store() {

    $file            = Input::file('file');
    $identifier      = date("Ymd") . " - " . Session::get('lastName') . "_" . Session::get('firstName');

    $destinationPath = base_path() . '/uploads/'. $identifier ;
    $extension       = $file->getClientOriginalExtension();
    $filename        = $identifier . " - " . uniqid() . "." . $extension;

    $upload_success = Input::file('file')->move($destinationPath, $filename);

    if( $upload_success ) {
        return Response::json('success', 200);
    } else {
        return Response::json('error', 400);
    }

}

我正在考虑改用这样的东西:

And I am thinking about using something like this instead:

    Session::put([
        'file' => Input::get('file'),
    ]);

但是,每当我检查Session时,上传文件后,"file"的值都会为"null".

But whenever I check my Session, after I uploaded a file, I get the value "null" for "file".

由于我每个Ajax都上传多个文件,所以我不确定是否会以某种方式破坏我将文件放入Session的方式.

Since I am uploading multiple files per Ajax, I am not sure if it somehow breaks the way I put files into the Session.

那么,如何将每个Ajax的多个文件保存到Laravel会话中?

So, how do I save multiple files per Ajax into the Laravel Session?

谢谢.

推荐答案

会话仅适用于少量的琐碎数据,而不适用于较大的数据和图像等文件.

Sessions are for small, trivial bits of data only, not large bits of data and files like images.

将图像存储在通常的目录中,然后在用户填写表格后将其移动到另一个目录中.有一个定期运行的垃圾收集"脚本,如果用户在一段时间后还没有填写完表格,它将清除第一个目录中的所有图像.

Store the image in a directory like normal, then move them to another directory if the user completes the form. Have a "garbage collection" script that runs periodically that cleans any images from the first directory in the case of a user hasn’t completed the form after some time.

您的句子只有那时我才想使用真实的服务器资源"毫无意义,就好像您要将文件保存到会话中那样会仍然使用服务器的资源.会话被写入磁盘.如果您将文件作为BLOB存储到数据库中,则也是一样(也不要这样做).您仍在使用服务器的资源.因此,将文件保存到会话的理论不会停止使用服务器的资源.

Your sentence, "only then I wanted to use real server resource" makes no sense, as if you were to save the file to a session then that would still use your server’s resource. Sessions are written to disk. Same thing if you were to store the file to the database as a BLOB (don’t do that either). You’re still using your server’s resources. So the theory of saving the file to the session doesn’t stop using your server’s resources.

这篇关于Laravel:将上传的文件保存到Session中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆