| 
 
| 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找不到文件。这个问题不是本文重点,详情不予细讲。
 
 复制代码<?php
        $ch = curl_init();
        $filepath = 'D:\Web\www\wuxiancheng.cn\wuxiancheng.txt';
        $basename = pathInfo($filepath, PATHINFO_BASENAME);
        $filesize = filesize($filepath);
        curl_setopt_array(
                $ch,
                array(
                        CURLOPT_URL => 'ftp://www.wuxiancheng.cn/books/' . $basename,
                        CURLOPT_USERPWD => 'USER:PASSWORD',
                        CURLOPT_PROTOCOLS => CURLPROTO_FTP,
                        CURLOPT_UPLOAD => true,
                        CURLOPT_INFILE => $filepath,
                        CURLOPT_INFILESIZE => $filesize,
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_HEADER => false,
                )
        );
        curl_exec($ch);
        $message = curl_errno($ch) === CURLE_OK ? 'success' : 'failure';
        echo $message;
?>
 | 
 |