Save file with put_contents file in folder

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

This works fine - save the file in the current folder, but if I try:

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

then I have an error:

Could not open stream: no such file or directory in

etc.

why? How to save this in a subfolder?

+6
source share
5 answers

Always use the full paths and make sure the directory is writable. You can also use copy directly from the 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); 
+10
source

Try leaving the first slash:

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

Check permissions if this still does not work.

+6
source

You should check if the folder has disappeared and if it is not created in 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 you want to use ABSOLUTE_PATH instead of RELATIVE

+2
source

 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.. 
+1
source
 $dir = "folder_name".$filename; 

you can use the above to simply put the contents in any file of any folder.

0
source

Source: https://habr.com/ru/post/947730/


All Articles