我想要的是什么:
我想检查<input type="file">
是否为空。
问题:
我知道它会检查它。我实际上认为它是缓存或其他东西,但无法找到位置
但是当我没有选择文件时,它也会进入if ...
这就是我的所作所为:
<?
if (empty($_FILES['picture']['name']))
{
}
else
{
//Does some file checks...
}
?>
这是整页代码:
<?
include 'core/init.php';
$id = $_POST['p_id'];
$title = $_POST['title'];
$content = $_POST['content'];
$u_id = uniqid();
if (empty($title))
{
$error[] = "Titel mag niet leeg staan";
}
if (empty($content))
{
$error[] = "Beschrijving mag niet leeg staan";
}
if (empty($_FILES['picture']['name']))
{
}
else
{
$target_dir = "images/projects/";
$file_name = $u_id . '.' . pathinfo($target_dir.$_FILES['picture']['name'],PATHINFO_EXTENSION);
$target_file = $target_dir . $file_name;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST['submit']))
{
$check = getimagesize($_FILES["picture"]["tmp_name"]);
if($check !== false)
{
$uploadOk = 1;
}
else
{
$error2[] = "Dit is geen geldig bestand";
$uploadOk = 0;
}
}
if (isset($_FILES['picture']) || $_FILES['picture']['error'] != UPLOAD_ERR_NO_FILE)
{
// Check if file already exists
if (file_exists($target_file))
{
$error[] = "Bestandnaam bestaat al, geef het een andere naam";
$uploadOk = 0;
}
// Check file size
if ($_FILES["picture"]["size"] > 5000000)
{
$error[] = "Bestand is te groot";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif")
{
$error[] = "Wij accepteren alleen JPG, JPEG, PNG & GIF bestanden";
$uploadOk = 0;
}
}
}
if (isset($error))
{
foreach($error as $error)
{
echo "<div id='project_error_list'></div>" . $error;
}
}
else
{
if (empty($_FILES['picture']['name']))
{
}
else
{
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
$error2[] = "Uploaden van het bestand geanuleerd";
// if everything is ok, try to upload file
}
else
{
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file))
{
//Do the rest
}
else
{
$error2[] = "Er is iets misgegaan met het uploaden van het bestand";
}
}
}
if (isset($error2) && !empty($error2))
{
foreach($error2 as $error)
{
echo "<div id='project_error_list'></div>" . $error;
}
}
else
{
echo edit_project($id, $title, $content, $file_name);
}
}
?>
<form class="form-horizontal" id="project_edit_form" enctype="multipart/form-data">
<div class="form-group">
<label for="picture" class="control-label">Thumbnail</label>
<input type="file" name="picture" class="form-control">
<label class="control-label">Huidige Thumbnail</label>
<img class="form-control" style="max-width:35%; height:auto;" src="images/projects/<?=(empty($data['picture'])) ? 'default.png' : $data['picture']?>">
<input type="hidden" name="p_id" value="<?=$id?>">
<input type="submit" id="save_project" value="Opslaan" class="btn btn-lg btn-primary" style="float:right;">
</div>
</form>
希望你们有足够的信息来帮助我。如果没有,请问我。
答案 0 :(得分:0)
找到了解决方案!
在我的完整php代码的底部,我检查$errro2
已设置,我需要将此if/else
放入我检查的部分move_upload_file
现在应该是这样:
<?
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file))
{
//Do the rest
if (isset($error2) && !empty($error2))
{
foreach($error2 as $error)
{
echo "<div id='project_error_list'></div>" . $error;
}
}
else
{
echo edit_project($id, $title, $content, $file_name);
}
}
else
{
$error2[] = "Er is iets misgegaan met het uploaden van het bestand";
}