使用PHP / Laravel确认后从temp获取文件

I have a form with a file to uplaod. All works find. But I don't want to move the file directly into a folder. After submit I show a confirm page and there I show the uploaded file with

header('Content-Type: image/x-png');
$file = file_get_contents(\Illuminate\Support\Facades\Input::file('restImg'));
$imgType = \Illuminate\Support\Facades\Input::file('restImg')->guessClientExtension();
echo sprintf('<img src="data:image/png;base64,%s" style="max-height: 200px"/>', base64_encode($file));

This works fine. After the confirmation I like to move the file to a folder. How can I move the file after the confirmation? The Input::get('file') is not available anymore.

You will have to store the file in the initial upload somewhere temporarily other than the default tmp directory.

The documentation for PHP file uploads says:

The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed

This means that moving onto the next request, the file will no longer be available.

Instead, move it to your own custom temp directory or rename it to something special, then keep the filename in the $_SESSION to persist it to the next request.

For Laravel, this should mean putting it in the /storage directory with something like this:

// Get the uploaded file
$file = app('request')->file('myfile');

// Build the new destination
$destination = storage_path() . DIRECTORY_SEPARATOR . 'myfolder';

// Make a semi-random file name to try to avoid conflicts (you can tweak this)
$extension = $file->getClientOriginalExtension();
$newFilename = md5($file->getClientOriginalName() . microtime()).'.'.$extension;

// Move the tmp file to new destination
app('request')->file('myfile')->move($destination, $newFilename);

// Remember the last uploaded file path at new destination
app('session')->put('uploaded_file', $destination.DIRECTORY_SEPARATOR.$newFilename);

Just remember to unlink() the file after the second request or do something else with it, or that folder will fill up fast.

Additional Reference: http://api.symfony.com/2.7/Symfony/Component/HttpFoundation/File/UploadedFile.html