25

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?

7
  • 1
    If it's on the local filesystem already, why not just copy($src, $dest)? Commented Jul 4, 2015 at 13:41
  • Why would /home/ be accessible per HTTP anyway? And what was the _POSTFIELDS supposed to do? Commented Jul 4, 2015 at 13:42
  • Now i have no vps to test the code,the assumption all source and target file in local machine can be changed when you give me the right code. Commented Jul 4, 2015 at 13:43
  • You'll need a file hosted at 127.0.0.1 with a server-side language that can take the posted field/file and put it on the local filesystem. Commented Jul 9, 2015 at 2:08
  • Why does this question now have a bounty? Is the given answer to use copy() not sufficient? Why not? What are you looking for? Commented Jul 14, 2015 at 11:45

7 Answers 7

8
+25

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.

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

Comments

3

It is a low level error.

curl_setopt($ch, CURLOPT_URL, "ftp://$usr:$pwd@$host$ftp_path/$dest");

Comments

1

try something like this and I feel instead of server directory path it would be http url.

// initialise the curl request
$request = curl_init('http://example.com/');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('test.txt')
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);

Comments

1

This code might help you:

<?php
    $rCURL = curl_init();
    curl_setopt($rCURL, CURLOPT_URL, 'http://www.google.com/images/srpr/logo11w.png');
    curl_setopt($rCURL, CURLOPT_HEADER, 0);
    curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);

    $aData = curl_exec($rCURL);
    curl_close($rCURL);
    file_put_contents('bla.jpeg', $aData);
    //  file_put_contents('my_folder/bla.jpeg', $aData); /*You can use this too*/

1 Comment

I want to put something into a url from local directory,not to download a url into my directory!
0

Try to specify the MIME type of the file sent like this

curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
       'file' => '@' . realpath('/home/test.mp4') . ';type=video/mp4'
    ));

Comments

0

The code you posted is for the client side. If you want to upload a file using HTTP, you HTTP server must be able to handle this upload request and save the file where you want. The “error message” is probably the server’s default web page.

Sample server-side code in PHP, for your reference:

<?php
if ($_FILES) {
    $filename = $_FILES['file']['name'];
    $tmpname = $_FILES['file']['tmp_name'];
    if (move_uploaded_file($tmpname,'/var/www/ok.mp4')) {
        print_r('ok');
    } else {
        print_r('failure');
    }
}

Comments

-3
curl -X POST -F "[email protected]" http://example.com/

You will also need a page that can process this request (POST)

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.