Hello!

The question is how to upload / transfer a file (as a pointer to it using the URL on my server) to another remote server. Below is the image upload code for image hosting using the input file form. It works remarkably. But the problem begins when I try to replace

 $ filename = $ img ['tmp_name'];

on

$filename = $image_pass; // где $image_pass == путь к файлу на сервере-доноре (типа http://server.com/images/image.jpg) 

After 5 hours of googling, an understanding appears that in $ _FILES (when using the file upload form) a certain structure is formed

 $ _FILES == Array ( 
 [name] => 1.jpg 
 [type] => image / jpeg 
 [tmp_name] => http://server.com/images/image.jpg
 [error] => 0 [size] => 60541 
 )

loadable file is stored in the internals of the $ _FILES array, then sent in parts by the POST method to a remote server.

How to imitate the work of the form, but send the handler only URL files and not the coded value from the $ _FILES array?
Help please, who can!

The working code is below:

 <!DOCTYPE html> <html> <head> <title>Image Upload Using Imgur API</title> </head> <body> <div id="content" style="margin-top:10px;height:100%;"> <h1>Image Upload Using Imgur API</h1> <form action="" enctype="multipart/form-data" method="POST"> Choose Image : <input name="img" size="35" type="file"/><br/> <input type="submit" name="submit" value="Upload"/> </form> <? if (isset($_POST['submit'])) { $img = $_FILES['img']; if ($img['name'] == '') { echo "<h2>An Image Please.</h2>"; } else { $filename = $img['tmp_name']; $client_id = "77777777777777777";//Your Client ID here $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename)); $pvars = array('image' => base64_encode($data)); $timeout = 30; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id)); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars); $out = curl_exec($curl); curl_close($curl); $pms = json_decode($out, true); $url = $pms['data']['link']; if ($url != "") { echo "<h2>Uploaded Without Any Problem</h2>"; echo "<img src='$url'/>"; echo $url; } else { echo "<h2>There's a Problem</h2>"; echo $pms['data']['error']['message']; } } } ?> </div> </body> </html> 

  • PDO superfluous. - Arnial

3 answers 3

You can not send a file url. You need to read the data from the files and send them.

The problem occurs because the fopen function expects the path to the local file and not the url.

When you upload a file through a form, it is first stored on the server in a temporary folder. tmp_name points to this temporary file, not url. Usually there is something like /tmp/some_random_letters .

If you make $filename = "./images/image.jpg"; Should start to work (I assume that the php file is at the root (i.e. available at http://server.com/<image_upload.php> )).

    If there is a url to the file available to the server, then you can

     $fileName = explode('/',$url); $fileName = $fileName[count($fileName)-1]; $result = file_put_contents($fileName,file_get_contents($url)); if($result) { echo 'OK'; } else { echo 'fail'; } 

      If you need to send the file as url, then it is logical to assume that the server will need to read it with this url :)

      Just remember, links to images should be complete, i.e. for example http://site.ru/images/image.jpg

       <!DOCTYPE html> <html> <head> <title>Image Upload Using Imgur API</title> </head> <body> <div id="content" style="margin-top:10px;height:100%;"> <h1>Image Upload Using Imgur API</h1> <form action="" enctype="multipart/form-data" method="POST"> Choose Image : <input name="img" size="35" type="text"/><br/> <input type="submit" name="submit" value="Upload"/> </form> <? if (isset($_POST['submit'])) { $img = $_POST['img']; if ($img == '') { echo "<h2>An Image Please.</h2>"; } else { $client_id = "77777777777777777";//Your Client ID here $data = file_get_contents($img); $pvars = array('image' => base64_encode($data)); $timeout = 30; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id)); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars); $out = curl_exec($curl); curl_close($curl); $pms = json_decode($out, true); $url = $pms['data']['link']; if ($url != "") { echo "<h2>Uploaded Without Any Problem</h2>"; echo "<img src='$url'/>"; echo $url; } else { echo "<h2>There's a Problem</h2>"; echo $pms['data']['error']['message']; } } } ?> </div> </body> </html>