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
Share on other sites

Thanks for that. How could I implement this into the code to be able to define a target directory? So it would display a list of existing directories on the upload page, and I could chose which one to upload the image to?

Link to comment
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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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; 
    } 

?>

Link to comment
Share on other sites

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; 
    }  ?>

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.