所以我有一个脚本允许文件一次上传:
<input style = "width: 90px;" name="book-image" type="file" id="image" value = "Upload">
<input style = "width: 90px;" name="book-preview" type="file" id="book" value = "Upload">
这是接收脚本:如果图像存在,它会测试预览(pdf)是否存在,然后运行 2 个函数来上传每个图像和 pdf。
if(file_exists($_FILES['book-image']['tmp_name']) || is_uploaded_file($_FILES['book-image']['tmp_name'])) {
//If there is a preview or non-free book
if((file_exists($_FILES['book-preview']['tmp_name']) || is_uploaded_file($_FILES['book-preview']['tmp_name']))) {
//valid - upload image, preview and add book
if($image = uploadBookImage() && $pdf = uploadPreviewPdf()) {
$newValues['imagefilename'] = $image;
$newValues['previewfile'] = $pdf;
if($qc->insertBook($newValues)) {
$message = "Added Book!";
} else{
$error .= "Couldn't add book";
}
}
}
}
$qc->insertBook($values); 将值添加到数据库中。
以下是上传功能:
function uploadBookImage() {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$gay = explode(".", $_FILES['book-image']['name']);
$extension = end($gay);
$target_path = "../images/books/";
$filename = $_FILES["book-image"]["name"];
$target_path = $target_path . basename($_FILES['book-image']['name']);
if ((($_FILES["book-image"]["type"] == "image/gif")
|| ($_FILES["book-image"]["type"] == "image/jpeg")
|| ($_FILES["book-image"]["type"] == "image/png")
|| ($_FILES["book-image"]["type"] == "image/pjpeg"))
&& ($_FILES["book-image"]["size"] < 2000000)
&& in_array($extension, $allowedExts)){
if (file_exists($target_path)) {
return false;
} else {
if(move_uploaded_file($_FILES['book-image']['tmp_name'], $target_path)) {
echo $filename;
return $filename;
}
}
} else {
return false;
}
}//function
function uploadPreviewPdf() {
$allowedExts = array("pdf");
$gay = explode(".", $_FILES['book-preview']['name']);
$extension = end($gay);
$target_path = '../previews/';
$filename = $_FILES['book-preview']['name'];
$target_path = $target_path . basename($_FILES['book-preview']['name']);
if((($_FILES['book-preview']['type'] == 'application/pdf'))
&& ($_FILES['book-preview']['size'] < 6000000)
&& in_array($extension, $allowedExts)) {
if(file_exists($target_path)) {
return false;
} else {
if(move_uploaded_file($_FILES['book-preview']['tmp_name'], $target_path)) {
return $filename;
}
}
} else {
return false;
}
}
问题:当我上传两者时,它会将图像名称的值 1 和 pdf 的文件名添加到数据库中。但是,如果我只上传图像,它会将图像名称添加到数据库中
上传图片时var dump的输出:输出图片文件名,上传图片和pdf时var dump的输出:输出图片文件名
我不知道为什么。请帮忙。