Is a file in $_FILES the same as a file_get_contents when referring to an image?

对着背影说爱祢 提交于 2021-01-28 21:10:55

问题


That being said, is $uploaded_image the 'same' variable using this:

$uploaded_image = $_FILES['picture']['tmp_name'];

or this?

$uploaded_image = file_get_contents($some_image_url);

So that I can do another upload of the image to a third party server the same way independently of which code above? uploadToOtherServer($uploaded_image) (I'm sure this function works in the first case, what about the second?) Thank you for your help.

PS: Considering both images are exactly the same

Edit: going further, I'm uploading to S3. This works:

$result = $s3->putObject(array(
                'Bucket'       => $bucket,
                'Key'          => $file_path,
                'ACL'          => 'public-read',
                'ContentType'  => $content_type,
                'SourceFile'   => $_FILES['profile_picture']['tmp_name']
));

would this also work?

$result = $s3->putObject(array(
                'Bucket'       => $bucket,
                'Key'          => $file_path,
                'ACL'          => 'public-read',
                'ContentType'  => $content_type,
                'SourceFile'   => file_get_contents($image_url)
));

回答1:


$_FILES['picture']['tmp_name'] and $some_image_url are both just paths pointing at a file. They are NOT the raw file data.

But if you had

$uploaded_image = file_get_contents($_FILES['picture']['tmp_name']);

then both would be equivalent - you'd have the binary data representing your file in the variable.




回答2:


Just pass the absolute file url ..

e.g. http://www.example.com/image.jpg so it reads as

$result = $s3->putObject(array(
                'Bucket'       => $bucket,
                'Key'          => $file_path,
                'ACL'          => 'public-read',
                'ContentType'  => $content_type,
                'SourceFile'   => 'http://www.example.com/image.jpg',
));

Following form login should get you going...

Reference: http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html

Concept from the following form should get you going.

<html>
  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  </head>
  <body>

  <form action="http://examplebucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
    Key to upload: 
    <input type="input"  name="key" value="user/user1/${filename}" /><br />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="success_action_redirect" value="http://examplebucket.s3.amazonaws.com/successful_upload.html" />
    Content-Type: 
    <input type="input"  name="Content-Type" value="image/jpeg" /><br />
    <input type="hidden" name="x-amz-meta-uuid" value="14365123651274" />
    <input type="text"   name="X-Amz-Credential" value="AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request" />
    <input type="text"   name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
    <input type="text"   name="X-Amz-Date" value="20130806T000000Z" />

    Tags for File: 
    <input type="input"  name="x-amz-meta-tag" value="" /><br />
    <input type="hidden" name="Policy" value='<Base64-encoded policy string>' />
    <input type="hidden" name="X-Amz-Signature" value="<signature-value>" />
    File: 
    <input type="file"   name="file" /> <br />
    <!-- The elements after this will be ignored -->
    <input type="submit" name="submit" value="Upload to Amazon S3" />
  </form>

</html>


来源:https://stackoverflow.com/questions/34161401/is-a-file-in-files-the-same-as-a-file-get-contents-when-referring-to-an-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!