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.


.



Relevant Pages

  • Re: require_once() driving me MAD ! - please HELP
    ... Each folder contains one or more files. ... I cannot, for the life of me, work out wtf is wrong iwth the script, and why I can't seem to use my directory structure. ... Did you mean to say that my include directive SHOULD be an absolute path to the file system ?. ...
    (comp.lang.php)
  • Re: Directory Size problem
    ... I've shutdown all potential ... sevices that could possibly be using teh directory e.g sendmail, ... If we could, we would put this directory structure on a separate file system since it was faster to unmount it, format it and remount it than it was to rm -rf the directory structure. ...
    (Fedora)
  • Re: OpenVMS Aliasing
    ... You can create logical names with multiple values, so the file system ... Clustering makes extensive use of this. ... a directory structure that is specific to a node and then ...
    (comp.os.vms)
  • Re: Directory Size problem
    ... I've shutdown all potential ... sevices that could possibly be using teh directory e.g sendmail, ... put this directory structure on a separate file system since it was ...
    (Fedora)
  • Re: Still no dirent.h in C1X
    ... there with multitasking capabilities should have a file system. ... Not all filesystems have directory structure as ... It doesn't mandate the existence of directories, ...
    (comp.lang.c)