【问题标题】:Get path of temp uploaded file with image extension in PHP在PHP中获取带有图像扩展名的临时上传文件的路径
【发布时间】:2015-04-11 01:03:08
【问题描述】:

我有一个表单可以让用户上传图片文件。

<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:\wamp\tmp\image.png

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

谢谢

【问题讨论】:

    标签: php laravel file-upload


    【解决方案1】:

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

    在第五层:

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

    【讨论】:

    • 谢谢,我明白你的意思,以后再分开和整理。我使用的 API 不是用于文件存储,而是用于向客户端发送图像/视频。 $w-&gt;sendMessageImage($destination, $imagePath) 是一种方法,我需要从表单中获取该文件的路径($imagePath)。我一直在寻找比获取文件名 $fileName = Input::file('uploadImageFile')-&gt;getClientOriginalName(); 并将其存储到文件夹并获取路径 $imagePath = Input::file('uploadImageFile')-&gt;move($destinationPath, $fileName); 更好的解决方案
    • 谢谢老兄,工作正常。 $result = $s3->putObject(array( 'Bucket' => 'safezone-s3-bucket', 'Key' => 'test.jpg', 'SourceFile' => $image->getPathname() )); .但不是在浏览器中显示照片,而是迫使我下载它。如何解决?
    • 您可能需要在返回内容之前设置标题$response-&gt;header('Content-Type', 'image/jpeg');
    • 这对我有帮助。谢谢
    【解决方案2】:

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

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

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

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

    【讨论】:

    • $file = Request::file('uploadImageFile'); 可能会给你错误,那么你应该使用$file = Input::file('uploadImageFile');
    • 是的,我同意你的看法@AshwaniPanwar
    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多