在PHP中获取具有图像扩展名的临时上传文件的路径

时间:2015-04-11 01:03:09

标签: php laravel file-upload

我有一个允许用户上传图像文件的表单。

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

当我尝试从临时文件夹中获取路径时出现问题。处理完请求后,我不需要图像文件。当我尝试使用类似的东西获取路径时

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

路径看起来像 C:\ wamp \ tmp \ phpA123.tmp。 我正在使用的API需要一个带有上传图像扩展名的路径 的 C:\瓦帕\ TMP \ image.png

除非我想将此图像复制到其他上传文件夹并使用它,否则无法找到方法。我不希望这些图像记录在服务器中

由于

2 个答案:

答案 0 :(得分:13)

了解正在使用的特定API会很有帮助,但是编写良好的文件存储API不应该依赖于用于存储文件的上传文件名。您应该能够使用API​​中的临时文件内容,并单独指定文件名。

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

答案 1 :(得分:4)

如果你想获得与 -

相同的输出

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

在Laravel中,你可以像@cdbconcepts -

所描述的那样做

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

相关问题