Forum Moderators: coopster

Message Too Old, No Replies

how i can create some directory and paths

         

shankimout

1:00 am on Mar 31, 2006 (gmt 0)

10+ Year Member



i have problem with mkdir function

for example i want to create these directory to my application root

temp\downloads\
temp\downloads\images
temp\downloads\pages
temp\downloads\pages\css
temp\downloads\pages\swf

mkdir doesnt support 2 or more dirs in every time

and i can't create the top directorys

please help me . thank you

scriptmasterdel

11:09 am on Mar 31, 2006 (gmt 0)

10+ Year Member



I have never even thought about adapting mkdir, but come to think of it you could put the dir in an array and then use the exploded (vai the slash) and then use path name to create a new directory on top of the sub dirs.

Something like this

<?
$dir = "images/tools/third/fourth";
$new_dir = explode("/",$dir);
foreach($new_dir as $num => $value)
{
if($num==0)
{
mkdir($value);
}
if($num==1)
{
mkdir($new_dir[0].'/'.$value);
}
if($num==2)
{
mkdir($new_dir[0].'/'.$new_dir[1].'/'.$value);
}
if($num==3)
{
mkdir($new_dir[0].'/'.$new_dir[1].'/'.$new_dir[2].'/'.$value);
}
}
?>

Hope this helps, or clears your mind a little

Del

scriptmasterdel

11:16 am on Mar 31, 2006 (gmt 0)

10+ Year Member



tested the script and it works, maybe a little validation is in order

<?
$dir = "images/and/tools/third/fourth";
$new_dir = explode("/",$dir);
foreach($new_dir as $num => $value)
{
if($num==0)
{
if(@mkdir($value))
{
echo '/'.$value.'/ was created';
}
else
{
echo '/'.$value.'/ already exists';
}
}
if($num==1)
{
if(@mkdir($new_dir[0].'/'.$value))
{
echo '/'.$new_dir[0].'/'.$value.'/ was created';
}
else
{
echo '/'.$new_dir[0].'/'.$value.'/ already exists';
}
}
if($num==2)
{
if(@mkdir($new_dir[0].'/'.$new_dir[1].'/'.$value))
{
echo '/'.$new_dir[0].'/'.$new_dir[1].'/'.$value.'/ was created';
}
else
{
echo '/'.$new_dir[0].'/'.$new_dir[1].'/'.$value.'/ already exists';
}
}
if($num==3)
{
if(@mkdir($new_dir[0].'/'.$new_dir[1].'/'.$new_dir[2].'/'.$value))
{
echo '/'.$new_dir[0].'/'.$new_dir[1].'/'.$new_dir[2].'/'.$value.'/ was created';
}
else
{
echo '/'.$new_dir[0].'/'.$new_dir[1].'/'.$new_dir[2].'/'.$value.'/ already exists';
}
}
echo '<br>';
}
?>

So that you know if the dir has been made or it already exists.

This could be the long way round it but this is all i can think of!

Cheers,
Del

coopster

3:58 pm on Mar 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Starting with PHP5 mkdir() [php.net] will create the directories recursively automagically for you when you set the optional parameter to TRUE. Before that, you have to create your own recursive operation.

shankimout

1:42 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



thank you , your code works well but when id create the dir temp\files and wants to create temp\files\images this code doesnt work ,

i write function for this

function dir2create($array,$start){
$tmp = "";
for($i=$start;$i < Count($array);$i++){
$tmp .= $array[$i] . "/";
}
return $tmp;
}
function makedir($dirname){
$exploded_dirname = explode("/",$dirname);
$changed_dir = 0;
foreach ($exploded_dirname as $key => $name){
if(!$created){
if($name!= ""){
if(!is_dir($name)){
mkdir($name);
$changed_dir++;
$created = false;
chdir($name);
}else{
chdir($name);
$changed_dir++;
$dir2create = dir2create($exploded_dirname,$key+1);
makedir($dir2create);
}
}
}
}
$txt_cd = "";
for($i=0;$i<$changed_dir;$i++){
$txt_cd .= "../";
}
chdir($txt_cd);
}