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?
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.
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.
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.
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.
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.
It is a low level error.
curl_setopt($ch, CURLOPT_URL, "ftp://$usr:$pwd@$host$ftp_path/$dest");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With