Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path of temp uploaded file with image extension in PHP

I have a form which lets the user upload the image file.

<div id="imageDiv">
             Image Path : <input class="imageOption" type="file" id= "uploadImageFile" name="uploadImageFile"  >
             </div>

The problem comes when I try to fetch the path from temp folder. I won't be needing the image files after processing the request. When I try to fetch the path with something like this

$imagePath =  $_FILES['uploadImageFile']['tmp_name'];

The path looks like C:\wamp\tmp\phpA123.tmp. The API I'm using would require a path with extension of an uploaded image like this C:\wamp\tmp\image.png

Couldn't figure out a way to do so unless I want to copy this image to some other upload folder and use it. I don't want these images logged in a server

Thanks

like image 690
user3491917 Avatar asked Apr 11 '15 01:04

user3491917


Video Answer


2 Answers

It would be helpful to know the specific API in use, but no well written file storage API should ever have to rely on the uploaded file name being used to store a file. You should be able to use the temp file contents in the API, and specify the file name separately.

In L5:

// Get the UploadedFile object
$file = Request::file('uploadImageFile');

// You can store this but should validate it to avoid conflicts
$original_name = $file->getClientOriginalName();

// This would be used for the payload
$file_path = $file->getPathName();

// Example S3 API upload
$s3client->putObject([
    'Key' => $original_name, // This will overwrite any other files with same name
    'SourceFile' => $file_path,
    'Bucket' => 'bucket_name'
]);
like image 87
chrisboustead Avatar answered Oct 11 '22 06:10

chrisboustead


If you want to get same output as -

$imagePath = $_FILES['uploadImageFile']['tmp_name'];

in Laravel, you can do something like this as described by @cdbconcepts -

$file = Request::file('uploadImageFile'); $imagePath = $file->getPathName()

like image 22
Sachin Vairagi Avatar answered Oct 11 '22 05:10

Sachin Vairagi