1

I want to upload the file from local to server using curl in php and without using the form (which is html).

My php version is 5.2.6.

I have tried many different way and make many research about (upload file to server using curl in php), but the solution in google cannot solve my problem.

Below is my code:

// open file descriptor
$fp = fopen ("user.com/user/fromLocal.txt", 'w+') or die('Unable to write a file'); 

// file to download
$ch = curl_init('C:/wamp/www/user/fromLocal.txt');
// enable SSL if needed
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

// output to file descriptor
curl_setopt($ch, CURLOPT_FILE, $fp);          
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set large timeout to allow curl to run for a longer time
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);     
curl_setopt($ch, CURLOPT_USERAGENT, 'any');
// Enable debug output
curl_setopt($ch, CURLOPT_VERBOSE, true);

echo $ch;
echo $fp;
curl_exec($ch);
curl_close($ch);                               
fclose($fp);

Expected output:

The file can upload to server and view.

Actual output:

Unable to write the file

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Possible duplicate of [Php upload image to remote server with cURL](https://stackoverflow.com/questions/35776859/php-upload-image-to-remote-server-with-curl) – Alessandro Bellanda Feb 11 '19 at 08:26
  • @AlessandroBellanda the solution didn't solve my problem – user10594468 Feb 11 '19 at 08:29
  • `user.com/user/fromLocal.txt` so you expect PHP to hack into the `user.com` server and write to a file? This isn't going to work. – Mike Doe Feb 11 '19 at 08:32
  • @emix sorry, i am new in php. Please forgive my mistake and thank for your reply. – user10594468 Feb 11 '19 at 08:36
  • You need to decide on an actual upload method. Just using curl doesn't quite cut it. The server needs to provide for a means to receive the file. Unless you elaborate on how exactly you configured e.g. PUT requests to go through, or set up a script to receive it, this isn't answerable. This code sample is commonly used for downloading, not uploading, btw. – mario Feb 11 '19 at 08:36
  • @mario bro, u are correct. I can download from server but cannot upload. I think i should create a form. Thank for your advice. – user10594468 Feb 11 '19 at 08:41
  • PD of [Issue uploading file with CURL, PHP and Apache on Windows](//stackoverflow.com/q/7651751) / [Send file via cURL from form POST in PHP](//stackoverflow.com/q/4223977) / [How enable PUT method in apache php 5.0](//stackoverflow.com/q/8808766) / [PHP: upload file from one server to another server](//stackoverflow.com/q/35311051) – mario Feb 11 '19 at 08:44
  • @mario bro, thank for your suggestion. If i upload file successful, i will comment here. – user10594468 Feb 11 '19 at 08:48
  • @mario problem solved! Thank. – user10594468 Feb 12 '19 at 07:41

1 Answers1

0

I think you miss important information.

fopen ("user.com/user/fromLocal.txt", 'w+')

this means nothing. To send a file to the server the server has to be configured to accept a POST request and you have to send the file through that endpoint.

If you have a form, do you send it to: "user.com/user/fromLocal.txt" ?? You have to create the form data with curl and send it to a server ready to accept your request. There are different ways to accomplish that. And the most simple is exactly to send a form using curl and not the HTML. But absolutly you cannot write a file like that in a server.

GDG612
  • 129
  • 1
  • 10