showkey
showkey

Reputation: 358

How to upload file into target directory with curl?

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?

Upvotes: 25

Views: 3481

Answers (7)

Puhua
Puhua

Reputation: 17

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');
    }
}

Upvotes: 0

PHP Worm...
PHP Worm...

Reputation: 4224

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*/

Upvotes: 1

pedala1
pedala1

Reputation: 35

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

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

Upvotes: -3

showkey
showkey

Reputation: 358

It is a low level error.

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

Upvotes: 3

Citizen SP
Citizen SP

Reputation: 1411

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.

Upvotes: 8

Ram Sharma
Ram Sharma

Reputation: 8809

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);

Upvotes: 1

Agung Santoso
Agung Santoso

Reputation: 68

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'
    ));

Upvotes: 0

Related Questions