Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file into target directory with curl?

Tags:

There is a file "/home/test.mp4" in my local machine,

I want to upload it into /var/www/ok.mp4 (the name changed when uploaded it). All the source file and target file are in the local machine.

How to fix my partial code ,to add something or to change something ?

<?php  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  curl_exec($ch);  ?> 

Think to Ram Sharma, the code was changed as the following:

<?php $request = curl_init('http://127.0.0.1/'); curl_setopt($request, CURLOPT_POST, true); curl_setopt(     $request,     CURLOPT_POSTFIELDS,     array(       'file' => '@' . realpath('/home/test.mp4')     )); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); echo curl_exec($request); // close the session curl_close($request); ?> 

An error message occur:

It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.

I have test with ftp_put,code1 works fine.

code1:

<?php     set_time_limit(0);     $host = 'xxxx';     $usr = 'yyyy';     $pwd = 'zzzz';     $src = 'd:/upload.sql';     $ftp_path = '/public_html/';     $des = 'upload_ftp_put.sql';     $conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");     ftp_login($conn_id, $usr, $pwd) or die("Cannot login");     $upload = ftp_put($conn_id, $ftp_path.$des, $src, FTP_ASCII);      print($upload); ?> 

The file d:/upload.sql in my local pc can be uploaded into my_ftp_ip/public_html/upload_ftp_put.sql with code1.
Now i rewite it with curl into code2.

code2:

<?php   set_time_limit(0);   $ch = curl_init();   $host = 'xxxx';   $usr = 'yyyy';   $pwd = 'zzzz';   $src = 'd:/upload.sql';   $ftp_path = '/public_html';   $dest = 'upload_curl.sql';   $fp = fopen($src, 'r');   curl_setopt($ch, CURLOPT_URL, 'ftp://user:pwd@host/'.$ftp_path .'/'. $dest);   curl_setopt($ch, CURLOPT_UPLOAD, 1);   curl_setopt($ch, CURLOPT_INFILE, $fp);   curl_setopt($ch, CURLOPT_INFILESIZE, filesize($src));   curl_exec ($ch);   $error_no = curl_errno($ch);   print($error_no);   curl_close ($ch); ?> 

The error info output is 6 .Why can't upload my local file into the ftp with curl?How to fix it?

like image 720
showkey Avatar asked Jul 04 '15 13:07

showkey


People also ask

How do I upload files using curl?

How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.

Can curl transfer files?

Uploading files using CURL is pretty straightforward once you've installed it. Several protocols allow CURL file upload including: FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, and TFTP. Each of these protocols works with CURL differently for uploading data.

How do you use the path in curl command?

I don't think you can give a path to curl, but you can CD to the location, download and CD back. Or using subshell. Both ways will only download if path exists. -O keeps remote file name.

How do I save a file using curl?

Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz. You can set the output file name while downloading file with the curl, execute: $ curl -o file. pdf https://your-domain-name/long-file-name.pdf.


2 Answers

Use copy():

copy('/home/test.mp4', '/var/www/ok.mp4'); 

It does not make sense to run the file through the network stack (which is what cURL does), on any protocol (HTTP, FTP, …), when the manipulation can be done locally, through the file system. Using network is more complicated and error-prone.

like image 183
Citizen SP Avatar answered Sep 20 '22 20:09

Citizen SP


It is a low level error.

curl_setopt($ch, CURLOPT_URL, "ftp://$usr:$pwd@$host$ftp_path/$dest"); 
like image 45
showkey Avatar answered Sep 19 '22 20:09

showkey