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  



$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!<br/>'.
        '<a href="javascript:history.go(-1);">'.
        '&lt;&lt Go Back</a>');
      }
    }
  }

put

echo $file['name'];

before that if statement

if (!in_array(end(explode(".",
strtolower($file['name']))),
$allowedExtensions))

and see the result.

this is the result, please dont go on the line no. as i have a form above this code in the file!


Notice: Undefined variable: error_codes in C:\xampp\htdocs\upload-file.php on line 49

Notice: Undefined variable: error_codes in C:\xampp\htdocs\upload-file.php on line 49

Notice: Undefined variable: error_codes in C:\xampp\htdocs\upload-file.php on line 49
Array
Warning: strtolower() expects parameter 1 to be string, array given in C:\xampp\htdocs\upload-file.php on line 77

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\upload-file.php on line 77
Array is an invalid file type!

try this

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

can you please confirm me where exactly should i paste the code? after which segment or block? :)

coz i think i am going somewhere wrong in placing the code and it is not really checking the condition when it should. Please i request you to go through the complete code once, maybe there you may find where i am going wrong. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.