473,409 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Move file to (potentially) non-existent directory

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
2 3395
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dustin | last post by:
I'm looking for a PHP script that supports multi-file upload. However, because my users could potentially be uploading hundreds of files, the existing multi-file upload scripts that people have...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
11
by: Elhanan maayan | last post by:
hi all.. i work in a company that deals with mainly with hardware devices via serial comm port. up untill most of the software was writeen in plain old dos c. that company is planning to move...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: poddar | last post by:
Hi, I am new to this group. I had just one small question Is it possible to get the file name back from file descriptor. i.e. I have written a function to overload the libc write using...
52
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
32
by: Olivier | last post by:
Dear all, I thought the code ----------------------------- pt_fichier_probleme = fopen(nom_fichier, "w"); if(pt_fichier_probleme == NULL){ message_warning_s ("Erreur l'ouverture du...
11
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines...
8
by: elmosik | last post by:
Is there any method to get this informations from php file? 1. Class methods 2. Check whether class in file is extended by 'XXX' or not (class MyClass extends XXX ...) - Class has same name...
6
by: walter-google | last post by:
Hi, I need to capture an event on a form file input button and present a dialog before allowing the user to browse for a file. (A customer needs this in a CMS that I'm customizing for them.) ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.