找回密码
 新建账号

PHP cURL Uploading to a URL without a file name!

[复制链接]
php 发表于 2017/12/4 22:44 | 显示全部楼层 |阅读模式
Uploading to a URL without a file name!错误出现在使用PHP通过cURL将文件上传到FTP服务器的过程中,是因为CURLOPT_URL指定的URL链接没有提供文件保存到FTP服务器上面时使用的文件名。假定要上传wuxiancheng.txt到ftp://www.wuxiancheng.cn/books/,CURLOPT_URL应该是ftp://www.wuxiancheng.cn/books/wuxiancheng.txt,而不是ftp://www.wuxiancheng.cn/books/,否则PHP将无法通过cURL上传文件到FTP服务器,调用curl_error()可以得到错误信息Uploading to a URL without a file name!
以下代码正确示范如何在PHP中通过cURL上传文件到FTP服务器。
对于Windows平台,如果PHP版本低于7.1并且代码文档编码为UTF-8,并且文件名或文件径包含中文,需要将文件路径转换为GBK,否则PHP找不到文件。这个问题不是本文重点,详情不予细讲。
  1. <?php
  2.         $ch = curl_init();
  3.         $filepath = 'D:\Web\www\wuxiancheng.cn\wuxiancheng.txt';
  4.         $basename = pathInfo($filepath, PATHINFO_BASENAME);
  5.         $filesize = filesize($filepath);
  6.         curl_setopt_array(
  7.                 $ch,
  8.                 array(
  9.                         CURLOPT_URL => 'ftp://www.wuxiancheng.cn/books/' . $basename,
  10.                         CURLOPT_USERPWD => 'USER:PASSWORD',
  11.                         CURLOPT_PROTOCOLS => CURLPROTO_FTP,
  12.                         CURLOPT_UPLOAD => true,
  13.                         CURLOPT_INFILE => $filepath,
  14.                         CURLOPT_INFILESIZE => $filesize,
  15.                         CURLOPT_RETURNTRANSFER => true,
  16.                         CURLOPT_HEADER => false,
  17.                 )
  18.         );
  19.         curl_exec($ch);
  20.         $message = curl_errno($ch) === CURLE_OK ? 'success' : 'failure';
  21.         echo $message;
  22. ?>
复制代码

手机版|轻松E站

GMT+8, 2024/3/19 15:51

快速回复 返回顶部 返回列表