Send file using cURL

After several hours of research, I could not solve the problem with PHP and cURL.

When I try to send the file directly from the form, the curl works fine.

<form method="post" action="" enctype="multipart/form-data"> <input name="file" type="file" /> <br /> <input name="submit" type="submit" value="Upload" /> </form> <?php $temp = $_FILES['file']['tmp_name']; $name = $_FILES['file']['name']; $post = array ( 'file' => '@'. $temp ); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); $exec = curl_exec ($ch); curl_close ($ch); ?> 

The above code is working correctly. When I try to use your form, the file is sent correctly.

My problem is that I need to send files that are already on the server.

I tried with the full path to go to the file "C: /xampp/htdocs/test/photos.zip", but for some reason does not work.

 $post = array ( 'file' => '@C:/xampp/htdocs/test/photos.zip' ); 

Does anyone know how I'm doing to send files already sent to the server?

Edit:

upload.php (server)

 <?php error_reporting( E_ALL ); $upload = $_FILES['file']; move_uploaded_file( $upload['tmp_name'], 'photos.zip'); ?> 

myuploadtest.php (localhost)

 <form action="" method="post" enctype="multipart/form-data"> <input name="file" type="file" /><br /> <input name="submit" type="submit" value="Upload" /> </form> <?php $temp = $_FILES['file']['tmp_name']; $name = $_FILES['file']['name']; $post = array( 'file' => '@'.$temp ); $url = "http://www.mysite.com/upload.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $exec = curl_exec($ch); curl_close($ch); ?> 

uploadcurl.php (localhost)

 <?php $post = array( 'file' => '@C:/xampp/htdocs/test/photos.zip' ); $url = "http://www.mysite.com/upload.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $exec = curl_exec($ch); curl_close($ch); ?> 

Thanks in advance.

+4
source share
2 answers

Here is what you can try:

  • Use relative paths, i.e. provide a path name relative to the php script used.

  • In the above code, I think you are missing the $ url declaration.

  • I don’t quite understand the question Does anyone know how I do to send files that have already been sent to the server? Which server are you uploading the file to? (localhost?)

If the relative path does not work, can you share the error messages you get?

+1
source

To send a file that is already on the server, find it with the full path on the server:

 $post = array( 'file' => '@'. $_FILES['file']['tmp_name'] ); 

This is exactly what you did first.

+1
source

All Articles