简体   繁体   中英

How can I upload an image from source URL to some destination URL?

Folks

I have an image at some server (SOURCE) ie http://stagging-school-images.s3.amazonaws.com/2274928daf974332ed4e69fddc7a342e.jpg

Now I want to upload it to somewhere else (DESTINATION) ie example.mysite.com/receiveImage.php

First, I am copying image from source to my local server and then uploading it to destination. It's perfectly working but taking too much time as it copy the image and then uploads... I want to make it more simple and optimized by directly uploading image from source URL to destination URL.

Is there a way to handle this ?

I am using php/cURL to handle my current functionality.

Any help would be very much appreciated.

Cheers !!

If example.mysite.com/receiveImage.php is your own service, then you may

  1. pass SOURCE URL to your PHP script as GET or POST parameter
  2. in PHP script, use file_get_contents() function to obtain image by URL, and save it to your storage

Otherwise it's impossible by means of HTTP.

However, there are some ways to increase files uploading speed a little:

  • If files are huge, you may use two threads: one for downloading (it will store all downloaded data to some buffer) and one for uploading (it will get all available data from buffer and upload it to site). As far as I know, this can't be done easily with PHP, because multi-threading is currently not supported yet.

  • If there are too many files, you may use many threads / processes, which will do download/upload simultaneously.

By the way, these means do not eliminate double traffic for your intermediate service.

其中一项服务可能在某处具有某种形式,该形式将允许您指定要从中接收/发送到的URL,但是没有通用的HTTP机制可以这样做。

The only way to transfer the image directly from source to destination is to initiate the transfer from either the source or the destination. You can't magically beam the data between these two locations without them talking to each other directly. If you can SSH login to your mysite.com server you could download the image directly from there. You could also write a script that runs on mysite.com and directly downloads the image from the source.

If that's not possible, the best alternative may be to play around with fread / fwrite instead of curl. This should allow you to read a little bit from the source, then directly upload that bit to the destination so download and upload can work in parallel. For huge files this should make a real difference, for small files on a decent connection it probably won't.

复制($ url,$ uploadDir。'/'。$ fileName);

创建两个文本字段,一个url,另一个php中的文件名,使用:uploadDir是文件目录的路径;)

 copy($url, $uploadDir.'/'.$fileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM