views:

1143

answers:

4

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?

A: 

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

Overbeeke
What I'm most worried about is its grabbing the file and the directory all in one go and I'm unsure of how to easily seperate them I guess.
+1  A: 

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.

jishi
A: 

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

okoman
+5  A: 
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.

gnud
Never noticed this before, thx :-)
okoman
Me neither. Recursive was apparently added in version 5. Coolness!
da5id