Hello,
I know that the ticket was opened some time ago, but I was looking for this full code for a while, and I found it only on the network, but nothing that worked completely, so here it is. Hope this helps some people.
I saw your post, and I had exactly the same problem, using most of the information found here - the main author is the code below with w3schools.
Here are two ways to do this:
1- your form or upload function is on the server that receives the file
// the index.php file on the server <br><br> <?php require_once 'upload.php'; ?> <!DOCTYPE html> <html><body> Formulaire de téléchargement d'une image <br><br> <form action="upload.php" method="POST" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> </body> </html>
upload.php file, in the same directory in this case
<?php // writing in a sublfolder called image $target_dir = "image/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { // give a name and direction to the image - i changed the name to test.jpg... $uploadfile = $_SERVER['DOCUMENT_ROOT'].$target_dir.'test.jpg'; echo "<br><br>"; echo $uploadfile; echo "<br><br>"; if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $uploadfile)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>
2- The second option, if your web server is different from the server receiving the file, here you need to have two files, one on each server:
Server that sends the file (your web server): send.php
<?php // I uploaded a file called sample2.jpeg from on server to another // again you can make functions out of this $target_url = 'http://192.168.56.103/receive.php'; //This needs to be the full path to the file you want to send. $file_name_with_full_path = realpath('./sample2.jpeg'); /* curl will accept an array here too. * Many examples I found showed a url-encoded string instead. * Take note that the 'key' in the array will be the key that shows up in the * $_FILES array of the accept script */ //$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path); $post = array('file_contents' => new CurlFile($file_name_with_full_path, 'text/plain' /* MIME-Type */,'' /*directory*/)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$target_url); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result=curl_exec ($ch); curl_close ($ch); echo $result; ?>
and the file on the server of the receiving server: receive.php
<?php // you can chage the destination path $uploaddir = realpath('./') . '/'; $uploadfile = $uploaddir . basename($_FILES['file_contents']['name']); echo "<br><br>"; print_r ($uploadfile); echo "<br><br>"; echo '<pre>'; if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); echo "\n<hr />\n"; print_r($_POST); print "</pr" . "e>\n"; ?> <br><br>Sources and references to other codes : <br>http://stackoverflow.com/questions/12667797/using-curl-to-upload-post-data-with-files <br>http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php <br>http://code.stephenmorley.org/php/sending-files-using-curl/ <br>http://www.w3schools.com/php/php_file_upload.asp
Thanks again to the true authors of 99% of the code, corrected with people's comments on the stackoverflow forum: derakkilgo and W3SCHOOLS.