25
votes

I want my php script to create an output file in a folder based on the date. The way I'm doing this is that its supposed to get the foldername/filename from a text file outputted by another program which I am unable to edit.

So the file its grabbing the data from looks like this:

data/newfolder/10302008/log_for_Today.txt | 24234234

Only with multiple lines, I just need the script to go through it line by line, grab the folder/filename and create an empty file with that name in that location.

The directories are all 777. Now I know how to create a new empty exe file in a folder but can't seem to figure out how to create the folder first then the exe inside of it, any ideas?

5

5 Answers

78
votes
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.

4
votes

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

2
votes

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!

1
votes

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.

0
votes

Can't you just do this by creating the dir with mkdir (http://nl.php.net/manual/en/function.mkdir.php) then chmod it 777 (http://nl.php.net/manual/en/function.chmod.php) the change directory with chdir (http://nl.php.net/manual/en/function.chdir.php) and then create the file (touch)?