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

use*_*917 6 php file-upload laravel

我有一个表单,让用户上传图像文件.

<div id="imageDiv">
             Image Path : <input class="imageOption" type="file" id= "uploadImageFile" name="uploadImageFile"  >
             </div>
Run Code Online (Sandbox Code Playgroud)

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

$imagePath =  $_FILES['uploadImageFile']['tmp_name'];
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

chr*_*ead 15

知道正在使用的特定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'
]);
Run Code Online (Sandbox Code Playgroud)


Zee*_*oft 6

如果你想获得与 - 相同的输出

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

在 Laravel 中,您可以按照 @cdbconcepts 的描述执行类似的操作 -

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