-1

I have this code and it shows me this errors, and I don´t know how to solve it!!

Warning: basename() expects parameter 1 to be string, array given in C:\AppServ\www\quieromiauto\subida.php on line 14

Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\quieromiauto\subida.php on line 19

File is not an image.

Sorry, file already exists.

Sorry, your file is too large.

Sorry, only JPG, JPEG, PNG & GIF files are allowed.

Sorry, your file was not uploaded.

<?
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES["subir"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["reg_auto"])) {
$check = getimagesize($_FILES['subir']["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
    }
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES['subir']["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES['subir']["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES['subir']["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

$brand = $_POST['marca'];
$modelo = $_POST['modelo'];
$kilometros = $_POST['km'];
$comb = $_POST['Combustible'];
$city = $_POST['ciudad'];
$price = $_POST['precio'];
$estado = $_POST['estado'];
$year = $_POST['anio'];
$f1 = $_FILES['subir']['name'][0];
$f2 = $_FILES['subir']['name'][1];
$f3 = $_FILES['subir']['name'][2];
$f4 = $_FILES['subir']['name'][3];
$f5 = $_FILES['subir']['name'][4];
$tecnica = $_POST['ficha'];
$provincia = $_POST['prov'];
$dest = $_POST['destacado'];
$fecha = date("d/m/Y");
$sql = "INSERT INTO tblPublicaciones (IdUsuario, Marca, Modelo, Año, Precio, Kilometros, Provincia, Ciudad, Destacado, Estado, Combustible, FichaTecnica, Foto1, Foto2, Foto3, Foto4, Foto5, FechaPublicacion, Consultas, Visitas, Actividad) VALUES('".$_SESSION['IdUsuario']."','".$brand."','".$modelo."','".$anio."','".$price."','".$kilometros."', '".$provincia."','".$city."', '".$dest."','".$estado."','".$comb."','".$ficha."','".$f1."','".$f2."','".$f3."','".$f4."','".$f5."', '".$fecha."')";
}
//echo "<script type='text/javascript'>window.location.href = 'home.php'</script>";
    ?>

And the html input file upload is:

<input type="file" class="foto1 form-control" name="subir[]" required multiple><br>
Community
  • 1
  • 1
  • You should do what you are doing now but for each file. $_FILES["subir"] is an array of files. So, something like foreach($_FILES["subir"] as $file) { //All your code but modified based on $file } – Mindastic Jul 08 '15 at 14:42
  • so, after the foreach I have to replace $_FILES["subir"] with $file ? – Franco Marziano Jul 08 '15 at 14:44
  • you're doing a multi-file upload using PHP's array naming hack. so EVERY part of $_FILES is going to be an array, and you need to iterate that array. – Marc B Jul 08 '15 at 14:44
  • Correct. Except for theses cases: $_FILES['subir']['name'][4] ... you should only need to use $file["name"] because you will be iterating them. – Mindastic Jul 08 '15 at 14:46
  • I´m doing the foreach: foreach ($_FILES['subir'] as $key => $value what do I have to put in $value ? – Franco Marziano Jul 08 '15 at 14:48
  • Its all fairly well documented in the manual [PHP:Uploading multiple files](http://php.net/manual/en/features.file-upload.multiple.php) and associated pages. A quick look in there should tell you what you need to know. Also take note of the `[error]` array that should be checked before doing anything with the rest of the array. – RiggsFolly Jul 08 '15 at 14:49
  • @RiggsFolly Do I have to leave the $value ? Please, help me with that.. – Franco Marziano Jul 08 '15 at 15:00
  • `$value` is filled by the `foreach`. Seems like a lengthy visit to the manual would be a useful way of passing some of your time. [The manual page for FOREACH](http://php.net/manual/en/control-structures.foreach.php) – RiggsFolly Jul 08 '15 at 15:05
  • Now, it throws me this error: Warning: getimagesize(n) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\quieromiauto\subida.php on line 23 File is not an image.Sorry, file already exists.Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded. You have to know I have already replaced all the $_FILES['subir'] with $file after the foreach – Franco Marziano Jul 08 '15 at 15:09
  • Sorry this is not ment to be unpleasant, but it seem like you have jumped in at the deep end of the pool and you cannot swim yet. Please go back and learn some very basic PHP before trying to do these more advanced things with PHP. I imaging you intend to keep asking until someone writes it for you, but I do not consider that helpful or what this site is intended to be for. – RiggsFolly Jul 08 '15 at 15:12
  • @FrancoMarziano check my answer, I added a way to use a foreach loop and I actually found a question for multiple file upload here: http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php – Coding Enthusiast Jul 08 '15 at 15:14
  • Thanks @CodingEnthusiast – Franco Marziano Jul 08 '15 at 15:19

1 Answers1

1

You are not looping through your multiple files. You are uploading it like it is one file. I believe you have to loop through the uploaded files in order to do the checking you want to do. You can't check if one group of files is an image. That doesn't make sense right? you probably want to do check each of them. So loop through.

$target_dir = "img/";
//count the number of files uploaded
$count = count($_FILES['subir']['name']);

$i = 0;
while($i<$count)
{
    $target_file = $target_dir . basename($_FILES["subir"]["name"]);
    $uploadOk = 1;
    //the rest of your code
    $i++; //increment i so you don't loop through forever
}

You can also use a foreach loop:

$target-dir = "img/";
$count=0; //count number of files
foreach ($_FILES['subir']['name'] as $filename) 
{
   $count=$count + 1;
   $target_file = $target_dir . basename($_FILES["subir"]["name"]);

   //your checking for validity code and uploading file code
   $target_file='';
}
Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
  • Hi, @CodingEnthusiast I have this error 5 times: Warning: getimagesize(C) [function.getimagesize]: failed to open stream: No such file or directory in C:\AppServ\www\quieromiauto\subida.php on line 26 File is not an image.Sorry, file already exists.Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded. But with different letters in getImageSize(C), getImageSize(D), getImageSize(H), getImageSize(i), getImageSize(j) and I dont know what I have to do.. – Franco Marziano Jul 08 '15 at 15:39
  • read the code you posted above. Those errors come from your code. your code checks if it is an image, and tells you . where did you copy this code @FrancoMarziano – Coding Enthusiast Jul 08 '15 at 15:43
  • This code is from w3schools @CodingEnthusiast http://www.w3schools.com/php/php_file_upload.asp – Franco Marziano Jul 08 '15 at 15:58
  • @FrancoMarziano read the tutorials, find something on youtube so they can explain what's happening because my believe is: you have no clue of what this code actually does. – Coding Enthusiast Jul 08 '15 at 16:32
  • but you can explain to me whats happening? – Franco Marziano Jul 08 '15 at 16:38