file_put_contents('image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));
Saving the file in current folder is working ok, but if I try
file_put_contents('/subfolder/image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));
I have an error:
failed to open stream: No such file or directory in […]
Why does this error occur? How can I save the file in a subfolder?
php $target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....
The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.
PHP code: saving a file with fopen / fwrite 'myfile1. txt'; $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); fwrite($fp, $data) or die("Couldn't write values to file!"); fclose($fp); echo "Saved to $file successfully!"; ?>
The file_put_contents() function writes a string to a file. The function returns the number of bytes that were written to the file, or FALSE on failure.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With