我在使用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);发布于 2012-06-14 04:24:52
$saveto是一个目录-你不能unlink()它,你必须rmdir()它。
发布于 2012-06-14 04:24:23
警告来自unlink,而不是cURL。看起来您正在尝试对目录执行此操作,而这是不允许的,并且根据文档,失败将产生警告。
您可以使用rmdir($dirname)删除目录,但除了具有适当的权限外,该目录必须为空才能执行此操作。
发布于 2012-06-14 04:25:13
那么对于starter unlink()是删除文件,rmdir()是删除目录。此外,目录必须为空。
当我看到这个警告:"Warning: unlink(images/)“时,它看起来像是您正在尝试删除一个使用错误函数的文件夹。
https://stackoverflow.com/questions/11022720
复制相似问题