【发布时间】:2020-01-10 12:32:42
【问题描述】:
我有这个 PHP 脚本,它可以将表单的内容与图像一起上传到数据库中。现在,我想让它在选择时上传所有内容(工作)并跳过上传图片部分并上传除图片以外的所有内容。
下面是上传图片到服务器的部分。
$time = time();
$uploader = $_SESSION['username'];
$target_dir = "../uploads/";
$target_file = $target_dir . $uploader . $time. basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["addpost-btn"])){
$check = getimagesize($_FILES["fileToUpload"]["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["fileToUpload"]["size"] > 2000000) {
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["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
//the rest is a database insert that works
当我尝试插入没有返回图像的表单内容时:
警告:getimagesize(): 第 14 行 C:\xampp\htdocs\vinhub\inc\addpost.inc.php 中的文件名不能为空
文件不是图像。抱歉,只允许使用 JPG、JPEG、PNG 和 GIF 文件。抱歉,您的文件未上传。
第 14 行是 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
我认为这是因为它需要选择文件,但我如何绕过它并在没有图像的情况下继续插入?
任何帮助将不胜感激,在此先感谢您
【问题讨论】:
-
if (isset($_FILES["fileToUpload"]))?
标签: php file-upload