• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Create directories if needed for move_uploaded_file

I want this to create all the directories and sub directories that are needed.

      if(move_uploaded_file($_FILES['file']['tmp_name'] , 'directory/subdir/a/123/file.txt' ))

So if any of the directories or sub directories ''directory/subdir/a/123/' do not exists they should all be created

0
hankknight
Asked:
hankknight
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
agamalCommented:
like this

<?php
$path = "/path/to/my/dir";
$path = explode("/",$path);
$pathroot = ".";
for ($i=1;$i<count($path);$i++) {
$pathroot = $pathroot."/".$path[$i];
if (!file_exists($pathroot)) {
mkdir($pathroot);
}
}
?>

Open in new window

0
 
hankknightAuthor Commented:
agamal, the first directory in your example gets lost.
<?php
 
makeDirs ('dir1/dir2/dir3/file.txt');
 
function makeDirs ($file) {
$path = pathinfo($file, PATHINFO_DIRNAME );
$path = explode("/",$path);
 
print_r($path);
 
$pathroot = ".";
for ($i=1;$i<count($path);$i++) {
$pathroot = $pathroot."/".$path[$i];
if (!file_exists($pathroot)) {
mkdir($pathroot);
chmod($pathroot, 0777);
}
}
}
 
?>

Open in new window

0
 
Ray PaseurCommented:
@hankknight: The important part of this question is, "How will you know what directory or subdirectory to use?"  If you can tell us that, we can probably give you the exact code example.  Best, ~Ray
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Also, consider using getcwd() to know something about your script location.  It is ALWAYS harder to keep your code straight if you are using relative addressing, so consider an architecture that lets you work with the WWW root as the anchor for your directory logic.
0
 
agamalCommented:
you mean the first dir is not created ....

then change
for ($i=1;$i<count($path);$i++) {


to
for ($i=0;$i<count($path);$i++) {

0
 
IceTreayCommented:
You might want to try this
<?php 
if (!is_dir("directory/subdir/a/123/")){
	mkdir("directory/subdir/a/123/");
	MoveFile("directory/subdir/a/123/file.txt");
}
else{
	MoveFile("directory/subdir/a/123/file.txt");
}
function MoveFile($directory){
	//Your other code
	if(move_uploaded_file($_FILES['file']['tmp_name'] , $directory))
	//Your other code
}
 
?>

Open in new window

0
 
IceTreayCommented:
The last code submitted won't work, but this is agamal's revised and does work
<?php
 
makeDirs ('directory/subdir/a/123/file.txt');
 
function makeDirs ($file) {
$path = pathinfo($file, PATHINFO_DIRNAME );
$path = explode("/",$path);
 
print_r($path);
 
$pathroot = ".";
for ($i=0;$i<count($path);$i++) {
$pathroot = $pathroot."/".$path[$i];
if (!file_exists($pathroot)) {
echo $pathroot;
mkdir($pathroot);
chmod($pathroot, 0777);
}
}
}
?>

Open in new window

0
 
agamalCommented:
where are we now ????
0
 
Ray PaseurCommented:
@agamal: I was wondering that, too!  Still hoping to get the answer to this:

How will you know what directory or subdirectory to use?
0
 
agamalCommented:
i find it more logical to tore it in fixed place on server .... what do you think Ray ... ?
0
 
Ray PaseurCommented:
Probably.  When i generate things for clients to download, I create a name based on the current DATETIME and their client ID, and I give them a link to it.  When I upload things for use in my web sites, I do a similar thing, saving all the uploaded files in a single directory, and saving the path information in the data base.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now