Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a file inside a folder that doesn't exist yet in php

People also ask

How create file if not exist in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How can I create a folder within a folder in PHP?

The mkdir() function is used to create directory in PHP. It is an inbuilt function in PHP. The mkdir() function creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure.

Can PHP create folders?

You can create a directory with PHP using the mkdir() function. mkdir("/path/to/my/dir", 0700); You can use fopen() to create a file inside that directory with the use of the mode w .

What is mkdir in PHP?

The mkdir() function creates a directory specified by a pathname.


if(!file_exists(dirname($file)))
    mkdir(dirname($file), 0777, true);
//do stuff with $file.

Use the third parameter to mkdir(), which makes it create directories recursively.


With

$directories = explode( '/', $path );

you can split the path to get single directory names. Then go through the array and create the directories setting chmod 777. (The system user, who executes php must have the ability to do that.)

$file = array_pop( $directories );
$base = '/my/base/dir';

foreach( $directories as $dir )
{

   $path = sprintf( '%s/%s', $base, $dir )
   mkdir( $path ); 
   chmod( $path, 777 );
   $base = $path;

}

// file_put_contents or something similar
file_put_contents( sprintf( '%s/%s', $base, $file ), $data );

The problem here is that you might not set chmod from your php script.

An alternative could be to use FTP. The user passes FTP login data to the script and it uses FTP functionality to manage files.

http://www.php.net/FTP


It's little too late but I found this question and I have a solution for this, here is an example code and it works good for me no matter how deep is your file. You should change directory separator for lines 1 and 3 if you're running it on Windows server.

$pathToFile = 'test1/test2/test3/test4/test.txt';
$fileName = basename($pathToFile);
$folders = explode('/', str_replace('/' . $fileName, '', $pathToFile));

$currentFolder = '';
foreach ($folders as $folder) {
    $currentFolder .= $folder . DIRECTORY_SEPARATOR;
    if (!file_exists($currentFolder)) {
        mkdir($currentFolder, 0755);
    }
}
file_put_contents($pathToFile, 'test');

Best regards, Georgi!


Create any missing folders using mkdir(), then create the empty file using touch().

You can use absolute paths in both cases, meaning:

mkdir('data');
mkdir('data/newfolder');
mkdir('data/newfolder/10302008');
touch('data/newfolder/10302008/log_for_Today.txt');

if you're curious about where it's starting-point it will be, you can use getcwd() to tell you the working directory.