我在实现多个if else语句时遇到了麻烦。我收到了一个意外的错误。
基本上我需要检查一个文件是否已经输入,如果已经输入,则需要检查它是否超过最大大小限制等,那么如果它满足所有条件,则需要检查它是否无论是jpeg,jpg还是png文件类型,最后数据库都会更新。
<?php
//this file uses the fields posted from the activity.html
//and stores it in the product_images table
$fileupload = $_FILES['userfile']['name'];
$filetype = $_FILES['userfile']['type'];
$filesize = $_FILES['userfile']['size'];
$tempname = $_FILES['userfile']['tmp_name'];
$filelocation = "images/$fileupload";
//make sure a file has been entered
if($tempname == "none") {
echo "<p>You must enter a file " . mysqli_error($conn) . "</p>" ;
}
else
{
if (!move_uploaded_file($tempname,$filelocation)) {
switch ($_FILES['userfile']['error'])
{
case UPLOAD_ERR_INI_SIZE:
echo "<p>Error: File exceeds the maximum size limit set by the server</p>" ;
break;
case UPLOAD_ERR_FORM_SIZE:
echo "<p>Error: File exceeds the maximum size limit set by the browser</p>" ;
break;
case UPLOAD_ERR_NO_FILE:
echo "<p>Error: No file uploaded</p>" ;
break;
default:
echo "<p>File could not be uploaded </p>" ;
}
}
else
{
if (($_FILES['userfile']['type'] == "image/jpeg") || ($_FILES['userfile']['type']== "image/jpg") || ($_FILES['userfile']['type']== "image/png") ){
//image file is valid
}
}
else
{
echo"Please upload file in one of these formats: jpeg,jpg or png.<a href='activity12-1.html'>Upload a file</a>";
}
}
else{
//Open the company database and make sure the product_images table is present
include ('companyDB.php');
$dbQuery = "INSERT INTO product_images(imageurl) VALUES ('$filelocation')";
if (mysqli_query($conn, $dbQuery))
{
echo"<h1>File Uploaded</h1>";
echo"<p><img src='$filelocation' height='281' width='500'></p>";
echo"The details of the uploaded file are shown below:<br/>";
echo"<b>File Name: </b>$fileupload<br/> ";
echo"<b>File type: </b>$filetype<br/> ";
echo"<b>File size: </b>$filesize bytes<br/> ";
echo"<b>Uploaded to: </b>$tempname<br/> ";
echo"<a href='activity12-1.html'>Add Another file</a>";
}
else
{
echo "<p>Couldn't add the file to the database " . mysqli_error($conn) . "</p>" ;
}
}
}
?>
答案 0 :(得分:0)
你错过了一个结束大括号。
这:
default:
echo "<p>File could not be uploaded </p>" ;
}
}
else
{
应该是这样的:
default:
echo "<p>File could not be uploaded </p>" ;
}
}
}
else
{
这就是保持缩进一致性很重要的原因。
答案 1 :(得分:0)
根据我的说法if-else语法是这样的:
if(condition)
{
}
else if(condition)
{
}
else if(condition)
{
}
else
{
}
你错过了@line 44的代码
else
{
if (($_FILES['userfile']['type'] == "image/jpeg") || ($_FILES['userfile']['type']== "image/jpg") || ($_FILES['userfile']['type']== "image/png") ){
//image file is valid
}
}
else
{
echo"Please upload file in one of these formats: jpeg,jpg or png.<a href='activity12-1.html'>Upload a file</a>";
}
}
else{
//