By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,722 Members | 1,874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,722 IT Pros & Developers. It's quick & easy.

Move file to (potentially) non-existent directory

P: n/a
mp-
Hi

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?

Thanks in advance.
Sep 6 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>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.
Sep 6 '08 #2

P: n/a
On 6 Sep, 03:17, gordonb.he...@burditt.org (Gordon Burditt) wrote:
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.
It'd be simpler to call mkdir() first, throw away the result then
check if the directory exists (cuts down on the amount of code needing
written)

C.
Sep 6 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.