-1

i'm trying to get an image from a URL and then save it to an images folder on my server. my site is hosted on bluehost and i'm using the following php script...

$url = "http://www.google.com/images/srpr/logo4w.png";
$save_name = "logo4w.png";
$save_directory = "/albums/images/art/";

if(is_writable($save_directory)) {
    file_write_contents($save_directory . $save_name, file_get_contents($url));
} else {
     exit("Failed to write to directory ".$save_directory);
}

but every time i try this i get the "Failed to write to directory error". I don't really know what i'm doing wrong. i could really use some help.

4

2 回答 2

0

尝试这个:

$url = "http://www.google.com/images/srpr/logo4w.png";
$save_name = "logo4w.png";
$save_directory = $_ENV["DOCUMENT_ROOT"]."/albums/images/art/"; // This ensures your path is correct
chmod($save_directory,0777); // This will make sure that directory is writeable

if(is_writable($save_directory)) {
    file_write_contents($save_directory . $save_name, file_get_contents($url));
} else {
     exit("Failed to write to directory ".$save_directory);
}
于 2013-05-16T16:37:39.387 回答
0

原来,实际的功能是

file_put_contents($save_directory . $save_name, file_get_contents($url));

并不是

 file_write_contents($save_directory . $save_name, file_get_contents($url));

谢谢你的帮助,尤其是安德鲁西

于 2013-05-16T18:16:59.493 回答