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