PHP多文件上传无效

时间:2015-07-08 14:39:40

标签: php html file-upload

我有这个代码,它显示了我的错误,我不知道如何解决它!

  

警告:basename()要求参数1为字符串,在第14行的C:\ AppServ \ www \ quieromiauto \ subida.php中给出数组

     

警告:getimagesize(Array)[function.getimagesize]:无法打开流:第19行的C:\ AppServ \ www \ quieromiauto \ subida.php中没有此类文件或目录

     

文件不是图像。

     

抱歉,文件已存在。

     

抱歉,您的文件太大了。

     

对不起,只有JPG,JPEG,PNG&允许使用GIF文件。

     

抱歉,您的文件未上传。

<?
$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>";
    ?>

html输入文件上传是:

<input type="file" class="foto1 form-control" name="subir[]" required multiple><br>

1 个答案:

答案 0 :(得分:1)

您没有遍历多个文件。您正在上传它,就像它是一个文件。我相信你必须遍历上传的文件才能进行你想要的检查。您无法检查一组文件是否为图像。这没有意义吗?你可能想检查每一个。所以循环。

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

你也可以使用foreach循环:

$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='';
}