Save file with file_put_contents in folder

Always use full paths and make sure the directory is writable. You can also use copy directly with URL

$url = 'http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg';
$dir = __DIR__ . "/subfolder"; // Full Path
$name = 'image.jpg';

is_dir($dir) || @mkdir($dir) || die("Can't Create folder");
copy($url, $dir . DIRECTORY_SEPARATOR . $name);

file_put_contents('../subfolder/image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));

add "../" in your string put into the file_put_contents function then it will work fine..

You should check if folder exsits and if not create this folder

$dir_to_save = "/subfolder/";
if (!is_dir($dir_to_save)) {
  mkdir($dir_to_save);
}
file_put_contents($dir_to_save.'image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));

also make sure that you want to use ABSOLUTE_PATH instead of RELATIVE


Try to leave out the first slash:

file_put_contents('subfolder/image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));

Check the access rights if this still doesn't work.

Tags:

Php

Apache