Creating a Folder When I Run File_Put_Contents()

Creating a folder when I run file_put_contents()

file_put_contents() does not create the directory structure. Only the file.

You will need to add logic to your script to test if the month directory exists. If not, use mkdir() first.

if (!is_dir('upload/promotions/' . $month)) {
// dir doesn't exist, make it
mkdir('upload/promotions/' . $month);
}

file_put_contents('upload/promotions/' . $month . '/' . $image, $contents_data);

Update: mkdir() accepts a third parameter of $recursive which will create any missing directory structure. Might be useful if you need to create multiple directories.

Example with recursive and directory permissions set to 777:

mkdir('upload/promotions/' . $month, 0777, true);

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);

Getting file_put_content into a folder

  1. make sure that the directory is present. file_put_contents doesn't create the directory if it is not present.

  2. Please specify what problems you are encountering on your code.

  3. change these parts

    $folder = time(); //make sure that time() returns string
    $folder = "blog/".$folder.'.html';
    file_put_contents($folder, $html_tekst);
    var_dump(file_get_contents($folder));

php file_put_contents no such file or directory occasionally

Finally, I found the reason from php source code. It's because the directory creation collision. For example:

  1. We have 2 processes to create directory "/home/test/fileputcontents/0" at the same time.
  2. Now only /home directory exists, we need create test/fileputcontents/0
  3. One process (we call A) has created test, is going to create fileputcontents,anther process (we call B) is going to create test again, but it already exists.
  4. Then process B stopping creating directory, because it thinks all directories exist
  5. When B tries to call file_put_contents before A creates all the directories, then it warns "No such file or directory", because fileputcontents/0 does not exist.

My solution, create directories because processes start.

file_put_contents create new file

You're only writing to the file if it exists and it's old. You should change the if so it writes to it if it doesn't exist as well.

<?php 
$cache_file = 'cachemenu/content.cache';

if(!file_exists($cache_file) || time() - filemtime($cache_file) > 86400) {

ob_start();
include 'includes/menu.php';
$buffer = ob_get_flush();
file_put_contents($cache_file, $buffer);
} else {
include 'cachemenu/content.cache';
}

?>

file_put_contents() or similar to create new photo

Base64 function to decode

function base64ToFile($base64String, $outputFile) {
$file = fopen($outputFile, "wb");
$data = explode(',', $base64String);
fwrite($file, base64_decode($data[1]));
fclose($file);

return $outputFile;
}

how can I create a file with file_put_contents that has group write permissions?

You might what to try setting the umask before calling file_put_contents : it will change the default permissions that will be given to the file when it's created.

The other way (better, according to the documentation) is to use chmod to change the permissions, just after the file has been created.


Well, after re-reading the question, I hope I understood it well...

file_put_contents fails to create file in the included path

The include path is the path that PHP searches when you include/require a file, not when you write to a file.

include_path Specifies a list of directories where the require, include, fopen(),
file(), readfile() and file_get_contents() functions look for files.

Just give the complete path:

file_put_contents("c:/users/shimantta/Desktop/test.txt", "Hello World. Testing!");

This will only work if the user running the script or the user running the webserver has permission to write to that directory.



Related Topics



Leave a reply



Submit