0

I have an array structure like the one below. I want to create a folder within folder using this array. How do I do this?

$folder=array(4) {
  [0]=>
  string(3) "img"
  [1]=>
  string(7) "dummies"
  [2]=>
  string(6) "slides"
  [3]=>
  string(6) "01.jpg"
}

I have tried like this but it does not solve my problem..

 $imglength=count($folder);
 $i=0;
 $fold=$i<($imglength-1);
 while(!file_exists($fold))
 {
  mkdir($fold);
 }
3
  • $fold=$i<($imglength-1); What is that? You know that this variable will just contain either true or false, right? Commented Feb 10, 2014 at 9:16
  • file_exists( $path_from_root . '/somedir/file/' . $fold ) check from root directory folder exits or not Commented Feb 10, 2014 at 9:17
  • how about array_map('mkdir', $array) ? (if file/dir exists, it will fail, but continue) Commented Feb 10, 2014 at 9:20

3 Answers 3

0

Well, this is one way of doing it. Probably not the most efficient way of doing it, but it works.

<?php
    $folders = Array(
        Array("img", "dummies", "slides", "01.jpg"),
        Array("img_24", "dummy", "slideshows", "65.png")
    );

    for ($i=0;$i<count($folders);$i++) {
        for ($j=0;$j<count($folders[$i]);$j++) {
            $path .= $folders[$i][$j] . "/";
            mkdir($path);
        }
        unset($path);
    }
?>

It basically reads the array and places each name into a single variable, which extends itself so that it creates a path. The JPEG file is not created, of course, but that's something you can do with file_put_contents or so, just give it another array value referring as "file" instead of the default "folder" definition. Then when it reads the definition it automatically creates a file instead of a folder and then writes in the bitmap data.

EDIT: For file creation, you can use file_put_contents like I explained above, I made a small piece of code to demonstrate the use.

<?php
    $folders = Array(
        Array("img", "dummies", "slides", Array("01.jpg", "11abcdefghijklmnopqrstuvxyz")),
        Array("img_24", "dummy", "slideshows", Array("65.png", "22abcdefghijklmnopqrstuvxyz"))
    );

    for ($i=0;$i<count($folders);$i++) {
        for ($j=0;$j<count($folders[$i]);$j++) {
            $path .= (gettype($folders[$i][$j]) != "array" ? $folders[$i][$j] . "/" : $folders[$i][$j][0]);
            if (gettype($folders[$i][$j]) != "array") {
                mkdir($path);
            }else{
                file_put_contents($path, (isset($folders[$i][$j][1]) ? $folders[$i][$j][1] : "nil"));
            }
        }
        unset($path);
    }
?>

The JPEG file's second value is used for the content of the file. You can put the bitmap data in that by reading the file you're using and then placing it in as a value. That way you get the image created properly.

Sign up to request clarification or add additional context in comments.

2 Comments

ThanKYou so much!!! last thing ahh i dn want to create that 01.jpg folder instead i want to put that image file into that folder... please do help me i tht too... please.. :)
Mhm. If you want to have multiple folder paths, then you can make multiple arrays within the base array. Then just add another for-loop for the sub arrays, this way you won't have to do the same for-loop for each one individually.
0

Just try

<?php 
$folder=array(array("img","dummies","slides","01.jpg"));


$imglength=count($folder);
 $i=0;
 $fold=$i<($imglength-1);

for($k=0;$k<$imglength;$k++)
{
    $sub_array_count=count($folder[$k]);
    $tmp='';
    for($j=0;$j<$sub_array_count;$j++)
    {
        if($j==0)
        {
            mkdir($folder[$k][$j]);


        }else
        {
            echo $folder[$k][($j-1)];
            echo "<br>";
            echo $tmp.=$folder[$k][($j-1)]."/";
            mkdir($tmp.$folder[$k][$j]);

        }
    }

}

?>

1 Comment

thank You for ur precious tym an precious work... :)
0
<html>
<body>

<form  method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

<?php

  if ($_FILES["file"]["error"] > 0)
  {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
  else
  {
        $tmp_name   =   $_FILES["file"]["tmp_name"];
  }


  $folders    =  array("img","dummies","slides");



foreach($folders as $folder)
{

  $folder_path .= $folder."/";


    while(!file_exists($folder_path))
    {

        mkdir($folder_path);

    }


}


move_uploaded_file($tmp_name, "$folder_path/".$_FILES['file']['name']);

3 Comments

ThanKYou so much!!! last thing ahh i dn want to create that 01.jpg folder instead i want to put that image file into that folder... please do help me i tht too... please.. :)
@user3048483 the image will be already uploaded via a form ??
@user3048483 ok updated code for you , hope this is what you were looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.