Home » Php » Get path of temp uploaded file with image extension in PHP

Get path of temp uploaded file with image extension in PHP

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

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'
]);

Questions:
Answers:

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()

Leave a Reply

Your email address will not be published. Required fields are marked *