seanj43 Posted May 5, 2011 Share Posted May 5, 2011 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 More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 to make a directory..you can use mkdir() Link to comment https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210922 Share on other sites More sharing options...
seanj43 Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210923 Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210924 Share on other sites More sharing options...
seanj43 Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210925 Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210930 Share on other sites More sharing options...
seanj43 Posted May 5, 2011 Author Share Posted May 5, 2011 Hmm that doesn't seem to do anything apart from return an error. Link to comment https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210950 Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 what error, and can i see your code Link to comment https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210952 Share on other sites More sharing options...
seanj43 Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1210955 Share on other sites More sharing options...
seanj43 Posted May 5, 2011 Author Share Posted May 5, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1211112 Share on other sites More sharing options...
seanj43 Posted May 6, 2011 Author Share Posted May 6, 2011 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 https://forums.phpfreaks.com/topic/235605-using-php-to-create-directories/#findComment-1211364 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.