我试-6ren">
gpt4 book ai didi

php - $_FILES 数组在未上传任何内容时不为空

转载 作者:IT王子 更新时间:2023-10-29 00:09:50 24 4
gpt4 key购买 nike

这是表格

form action="index.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>

我试图仅在 if(!empty($_FILES['image'])){ 时才运行我的代码,但由于某种原因,在未提交任何文件时数组不为空并且只需点击提交即可。

如果对您有帮助,这是其余代码,谢谢。

<html>

图片上传

    <form action="index.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>

<?php

include 'connect.php';

if(!empty($_FILES['image'])){
echo $_FILES['image']['error'];
$allowed = array('jpg', 'gif', 'png', 'jpeg');
$count = 0;

foreach($_FILES['image']['name'] as $key => $name){
$image_name = $name;
$tmp = explode('.', $image_name);
$image_extn = strtolower(end($tmp)); //can only reference file
$image_temp = $_FILES['image']['tmp_name'][$count];
$filesize = filesize($_FILES['image']['tmp_name'][$count]);
$count = $count +1;

if(count($_FILES['image']['tmp_name']) > 5){
echo "You can upload a maximum of five files.";
break;
}

else if(in_array($image_extn, $allowed) === false){
echo $name." is not an allowed file type<p></p>";
}

else if($filesize > 1024*1024*0.3){
echo $name." is too big, can be a maximum of 3MB";
}

else{

$image_path = 'images/' . substr(md5($name), 0, 10) . '.' . $image_extn;


move_uploaded_file($image_temp, $image_path);

mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image_path')") or die(mysql_error());

$lastid = mysql_insert_id();
$image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
$image_link = mysql_fetch_assoc($image_link);
$image_link = $image_link['image'];
echo "Image uploaded.<p></p> Your image: <p></p><a href = $image_link>$image_path</a>";
}

}
}

else{
echo "Please select an image.";
}

?>

最佳答案

这就是 $_FILES 数组在没有上传时的样子

Array ( [image] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

所以它永远不会是空的。

错误码4 [error] => 4表示没有上传文件,错误码code 0表示没有错误,上传了文件可以查看

if($_FILES['image']['error']==0) {
// file uploaded, process code here
}

这里是 another answer在 SO 上。

关于php - $_FILES 数组在未上传任何内容时不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12538718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com