Jump to content

Using PHP to create directories


seanj43

Recommended Posts

I am using the following code:

 

<?php 
$target = "images/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?> 

 

I am using this to upload photos to the directory called 'images'.

 

How could I change this code so that I can choose an existing directory to upload it to and also create a new directory to upload it to if I wanted?

 

 

Link to comment
https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/
Share on other sites

you would simply make the dir, then use move_uploaded_file as you already have, into the new dir

$structure = './depth1/depth2/depth3/';

mkdir($structure, 0777, true); //note that you can choose what mode you want to make it
move_uploaded_file($_FILES['uploaded']['tmp_name'], $structure);

thats just a very basic example...but you can incorporate that however you want

Thank you I have sorted this out now, but it has ruined another code I'm running.

 

<?php $files = glob("images/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i]; echo '<img src="'.$num.'" alt="random image">'."  "; } ?>

 

I was using this code to display all images from the directory 'images', but now I am using multiple sub-directories within the parent directory 'images' to store my images. How can I change this code so it will display all images in the directory 'images' including the contents of all sub-directories?

 

i got this from php.net...should help...it retrieves all the files from a directory and its subdirectories, excludes files that are in the exempt array

<?php 

    function getFiles($directory,$exempt = array('.','..','.ds_store','.svn'),&$files = array()) { 
        $handle = opendir($directory); 
        while(false !== ($resource = readdir($handle))) { 
            if(!in_array(strtolower($resource),$exempt)) { 
                if(is_dir($directory.$resource.'/')) 
                    array_merge($files, 
                        self::getFiles($directory.$resource.'/',$exempt,$files)); 
                else 
                    $files[] = $directory.$resource; 
            } 
        } 
        closedir($handle); 
        return $files; 
    } 

?>

There is no error now, but the code doesn't appear to do anything. I'm probably missing something really obvious...

 

<?php 

    function getFiles($directory,$exempt = array('.images'),&$files = array()) { 
        $handle = opendir($directory); 
        while(false !== ($resource = readdir($handle))) { 
            if(!in_array(strtolower($resource),$exempt)) { 
                if(is_dir($directory.$resource.'/')) 
                    array_merge($files, 
                        self::getFiles($directory.$resource.'/',$exempt,$files)); 
                else 
                    $files[] = $directory.$resource; 
            } 
        } 
        closedir($handle); 
        return $files; 
    }  ?>

 

Ok I seem to have found a way around this using this code:

 

<?php

$dir = "images/";

if ($opendir = opendir($dir) )
{
   while  ( ($file = readdir($opendir) )  !== FALSE)
   {
      if ($file!="."&&$file!="..")
         echo "$file";
   }
}

?>

 

This code returns a list of sub-directory names within the parent directory of 'images'.

 

How could I then use this information to get the code to then read the sub-directories and output the filenames of the contents of the sub-directories?

 

I figure I could use $file (sub-directory name), but how? I can't get my head around it.

 

Ok, I feel I am so close to solving this...

 

<?php

$dir = "images/";

if ($opendir = opendir($dir) )
{
   while  ( ($file = readdir($opendir) )  !== FALSE)
      {
      if ($file!="."&&$file!="..")
      echo "<img src = '$dir$file/FILENAME.jpg'";
      }
}

?>

 

This returns an image at mysite.com/images/subdirectory/FILENAME.jpg

 

How can I get it to automatically include the filename in the link?

 

I have tried readdir(images/$file) but it doesn't work for some reason.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.