php 在PHP中获取带有图像扩展名的临时上传文件的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29572850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get path of temp uploaded file with image extension in PHP
提问by user3491917
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
路径看起来像C:\wamp\tmp\phpA123.tmp。我使用的 API 需要一个带有上传图像扩展名的路径,如 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
谢谢
回答by chrisboustead
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.
了解正在使用的特定 API 会很有帮助,但编写良好的文件存储 API 不应依赖于用于存储文件的上传文件名。您应该能够使用 API 中的临时文件内容,并单独指定文件名。
In L5:
在 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'
]);
回答by Zeeweesoft
If you want to get same output as -
如果你想获得相同的输出 -
$imagePath = $_FILES['uploadImageFile']['tmp_name'];
$imagePath = $_FILES['uploadImageFile']['tmp_name'];
in Laravel, you can do something like this as described by @cdbconcepts -
在 Laravel 中,您可以按照@cdbconcepts 的描述执行类似操作 -
$file = Request::file('uploadImageFile');
$imagePath = $file->getPathName()
$file = Request::file('uploadImageFile');
$imagePath = $file->getPathName()