Upload multiple files, create a directory, save files in the new directory

First to upload files we need to create a form:

[code]

[/code]

ok first take notice of the input name uploads[], the [] symbols is telling php that it will be an array of files so when we collect the info in upload-file.php we have to treat everything as an array
so next we need to create upload-file.php:

<?php  

$id=rand(1,9999999); //this will give us a random value to create a unique directory

if(!is_dir("uploads/".$id)){   //this checks to make sure the directory does not already exist
mkdir("uploads/".$id, 0777, true); //if the directory doesn't exist then make it
chmod("uploads/".$id, 0777);  //chmod to 777 lets us write to the directory
}
$uploaddir='uploads/' . $id .'/';   //lets put the directory into a variable notice added slash on end 

foreach($_FILES["uploads"]["name"] as $bla=> $boo){      //we have to do a loop to get all the filenames
$file=$uploaddir.$boo;  //we will check the filename in the upload directory, see if it exists
if (file_exists($file)) {   //if it exists then ......
die("Filename already exists, please rename this file");   //if filename exists in the directory then we die!!! :P
}
}

Now that we checked to see if it is in the directory and it wasn’t so we created the directory, now we need to take the uploaded info and save it to the directory:

foreach ($_FILES["uploads"]["error"] as $key => $error) {

if ($error == UPLOAD_ERR_OK) {
echo"$error_codes[$error]";   // let you know if there was an error on any uploads
move_uploaded_file(      //php function to move the file
$_FILES["uploads"]["tmp_name"][$key],     //from the temporary directory 
$uploaddir. $_FILES["uploads"]["name"][$key]   //to the directory you chose
) or die("Problems with upload");
}

   }

if there was no errors then now we have our file in our directory if you want to add a link to the file you could do something like:

foreach($_FILES["uploads"]["name"] as $bla=> $boo){
$file=$uploaddir.$boo;

echo"<a href=".$file.">Click here to see".$file."<br>";
}

this is a simple script with no validation, I would suggest that you validate everything you allow to be uploaded to your server else be hacked!

Here is a validation to make sure they are image file extensions:

$allowedExtensions = array("jpg","jpeg","gif","png"); foreach ($_FILES as $file) { if ($file['tmp_name'] > '') { if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { die($file['name'].' is an invalid file type!<br/>'. '<a href="javascript:history.go(-1);">'. '&lt;&lt Go Back</a>'); } } }

Hope this tutorial helps somebody!!!

Very nice. Thanks for posting.

In this code to upload multiple images to a different folder each time, it creates a new folder and moves files to it, but the validation part is not working ! if i remove the validation part the files are uploaded correctly!!

I have mentioned using comments that in which area the problem i think is coming!

If someone can then please help!

the error is:

Warning: strtolower() expects parameter 1 to be string, array given in C:\xampp\htdocs\upload-file.php on line 66

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\upload-file.php on line 66

here’s the code:

[php]

<?php $contact_name = $_POST['contact_name']; $id=rand(1,9999999); //this will give us a random value to create a unique directory if(!is_dir("uploads/".$id)){ //this checks to make sure the directory does not already exist mkdir("uploads/".$id.$contact_name, 0777, true); //if the directory doesn't exist then make it chmod("uploads/".$id, 0777); //chmod to 777 lets us write to the directory } $uploaddir='uploads/' . $id.$contact_name.'/'; //lets put the directory into a variable notice added slash on end foreach($_FILES["uploads"]["name"] as $bla=> $boo){ //we have to do a loop to get all the filenames $file=$uploaddir.$boo; //we will check the filename in the upload directory, see if it exists if (file_exists($file)) { //if it exists then ...... die("Filename already exists, please rename this file"); //if filename exists in the directory then we die!!! :P } } foreach ($_FILES["uploads"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { echo"$error_codes[$error]"; // let you know if there was an error on any uploads move_uploaded_file( //php function to move the file $_FILES["uploads"]["tmp_name"][$key], //from the temporary directory $uploaddir. $_FILES["uploads"]["name"][$key] //to the directory you chose ) or die("Problems with upload"); } } /* THE PROBLEM IS IN HERE SOMEWHERE BECAUSE WHEN I REMOVE THIS PART, THE SCRIPT RUNS SUCCESSFULLY */ $allowedExtensions = array("jpg","jpeg","gif","png"); foreach ($_FILES as $file) { if ($file['tmp_name'] > '') { if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { die($file['name'].' is an invalid file type!
'. ''. '<&lt Go Back'); } } } [/php]

Try uploading the files to a Web Server. I have seen this error being displayed time and time again when using a local testing server such as XAMP or WAMP, and once uploaded to a live Web Server, the problem goes away.

So a lot of times, there’s not issue with the code, but it has more to do with the local server resources and its set up.

plintu,
Very nice post! I boosted your Karma! I wish everyone posted like that. Lots of details and explanations!
And, someone else just asked me about this very item, so sending them here… Thanks…

Sponsor our Newsletter | Privacy Policy | Terms of Service