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

9
  • Possible duplicate of Php upload image to remote server with cURL Commented Feb 11, 2019 at 8:26
  • @AlessandroBellanda the solution didn't solve my problem Commented Feb 11, 2019 at 8: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. Commented Feb 11, 2019 at 8:32
  • @emix sorry, i am new in php. Please forgive my mistake and thank for your reply. Commented Feb 11, 2019 at 8: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. Commented Feb 11, 2019 at 8:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.