使用 curl 将图像下载到我的本地主机时出现错误。我已经确保权限很好并且目录存在。我不知道接下来要检查什么。这是我得到的错误
Warning: unlink(images/) [function.unlink]: Operation not permitted in /Applications/MAMP/htdocs/test/image/instapaper.php on line 47
这是我的代码:
$saveto = 'images/';
if ( !file_exists($saveto) ) {
mkdir ($saveto, 0777, true);
}
$url = 'http://img.ffffound.com/static-data/assets/6/4dea2e91338b67cd926140acde1962403c3bf4c2_m.jpg';
echo getcwd() . "\n";
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto) && is_writable($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
if(fwrite($fp, $raw)==false){
echo 'no dice';
return;
}
fclose($fp);
**解决了
下面是工作代码。它基本上看起来像我试图将图像保存为我指定的目录位置。这已被清除,我已经提供了一个图像名称来保存文件。在最终脚本中,图像名称将从 url 中获取。
$url = 'http://img.ffffound.com/static-data/assets/6/4dea2e91338b67cd926140acde1962403c3bf4c2_m.jpg';
$saveto = 'images/';
$image_name = 'image.jpg';
$full_path = $saveto . $image_name;
if ( !file_exists($saveto) ) {
mkdir ($saveto, 0777, true);
}
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($full_path) && is_writable($full_path)){
unlink($full_path);
}
$fp = fopen($full_path,'x');
if(fwrite($fp, $raw)==false){
echo 'no dice';
return;
}
fclose($fp);