如何使用curl将文件上传到目标目录?

25

我的本地机器上有一个文件"/home/test.mp4"

我想将它上传到/var/www/ok.mp4(上传后名称已更改)。所有源文件和目标文件都在本地机器上。

如何修复我的部分代码,添加一些内容或更改一些内容?

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

?>

感谢Ram Sharma,代码已更改为以下内容:

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

出现错误信息:

它起作用了!
这是该服务器的默认网页。
Web服务器软件正在运行,但尚未添加任何内容。

我已经使用ftp_put测试过,code1可以正常工作。

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

我的本地电脑中的文件d:/upload.sql可以使用code1上传到my_ftp_ip/public_html/upload_ftp_put.sql
现在我用curl将其重写为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);
?>

错误信息输出为6。为什么我无法使用curl将本地文件上传到ftp?如何修复?


1
如果文件已经在本地文件系统上,为什么不直接使用copy($src, $dest)呢? - mario
为什么/home/会通过HTTP访问?而_POSTFIELDS原本的作用是什么? - mario
你需要一个托管在http://127.0.0.1上的文件,并使用能够接收提交的字段/文件并将其放置在本地文件系统中的服务器端语言。 - ThatOneDude
如果我有VPS来完成一些工作怎么样? - showkey
你没有清楚地描述问题。你想要达到什么目的?为什么你认为cURL是正确的工具,或者它能够完成这个任务呢? - Palec
显示剩余2条评论
7个回答

8
使用 copy() 函数:
copy('/home/test.mp4', '/var/www/ok.mp4');

将文件通过网络堆栈(cURL 所做的)运行,无论使用什么协议(HTTP、FTP等),都是没有意义的,因为可以通过本地文件系统进行操作。使用网络更加复杂且容易出错。


3

这是一个低级错误。

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

1
这段代码可能会对你有所帮助:
<?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*/

我想把本地目录中的内容放入URL,而不是把URL下载到我的目录中! - showkey

1
尝试像这样做,我感觉不是服务器目录路径,而是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);

0

尝试像这样指定发送文件的MIME类型

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

0
你贴出的代码是客户端代码。如果你想使用HTTP上传文件,你的HTTP服务器必须能处理这个上传请求并将文件保存在你要的位置。"错误信息"可能是服务器的默认网页。
以下是PHP的示例服务器端代码,供你参考:
<?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');
    }
}

-3
curl -X POST -F "image=@test.mp4" http://example.com/

您还需要一个能够处理此请求(POST)的页面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接