Reputation: 2079
I am getting an error using curl to download an image to my localhost. I've made sure the permissions are good and the directory exists. I'm at a loss for what to check next. This is the error I'm getting
Warning: unlink(images/) [function.unlink]: Operation not permitted in /Applications/MAMP/htdocs/test/image/instapaper.php on line 47
Here is my code:
$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);
**SOLVED
Below is the working code. It basically looks like I was trying to save the image as the directory location I had specified. That's been cleared up and I've got an image name provided to save the file to. In the final script the image name will be taken from the 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);
Upvotes: 2
Views: 709
Reputation:
Well for starter unlink() is to remove files, rmdir() is to remove directories. Also the directory must be empty.
When I look at this warning: "Warning: unlink(images/)" it looks like you're trying to delete a folder with the wrong function.
Upvotes: 0
Reputation: 65284
$saveto
is a directory - you can't unlink()
it, you must rmdir()
it.
Upvotes: 4
Reputation: 3270
The warning is with unlink
, not the cURL. And it looks like you're trying to do it to a directory, and that's not permitted, and, according to the docs, a failure will produce a warning.
You can use rmdir($dirname)
to remove a directory, but as well as the proper permissions, the directory must be empty for this to work.
Upvotes: 0