【发布时间】:2019-09-04 04:53:22
【问题描述】:
从 url 下载文件时出现 404 Not Found。 我想从 url 链接下载文件。
我尝试在 PHP 中使用 curl。
$output_filename = "text.jpg";
$imageUrl = "https://m.media-amazon.com/images/M/MV5BZjZhYTkyMjgtNjFkZi00NDAyLTk5ZDgtMDYwZTBkYTI1ODllXkEyXkFqcGdeQXVyNTc5OTMwOTQ@._V1_.jpg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
print_r($result); // prints the contents of the collected file before writing..
// the following lines write the contents to a file in the same directory (provided permissions etc)
$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);
当我运行脚本时,它会为我返回 404 not found 未找到文件的警告。 我也尝试从互联网上找到解决方案,但没有得到确切的结果。 我认为由于从 正则表达式 url 链接下载文件,它会发出警告。
在浏览器中粘贴图片网址时文件存在,如下图所示。
【问题讨论】:
-
它说找不到文件,因为您尝试下载的文件不存在。
-
是的,我尝试使用 php 中的 curl 从 url 下载文件。我尝试过的邮政编码。
-
尝试把url图片链接放到你的浏览器中,它不存在。
-
否,文件存在,请复制图片链接($imageUrl)并粘贴到浏览器地址栏按回车,显示图片。