Re: Move file to (potentially) non-existent directory



I would like to do the following:

rename('/home/testfile.txt', '/home/to/a/potentially/non-existent/
directory/testfile.txt');

PHP apparently won't auto-create the directory structure if it doesn't
already exist on the file system. Instead, it throws an error when
the directory doesn't already exist, stating "No such file or
directory".

Does anyone know of a way around this?

Check if the directory structure exists (is_dir(), stat(), filetype(),
or whatever), and if not, create it (mkdir). You may want to make
this recursive (e.g. if you want to create /a/b/c/d/e/file.txt, check
/a/b/c/d/e, and if it doesn't exist, check /a/b/c/d before trying to
create /a/b/c/d/e.) As of PHP 5.0.0, mkdir() accepts a recursive flag
so you don't need to code the recursive stuff.

You might run into permission problems and find this impossible.


.