使用If语句上载PHP文件验证

使用If语句上载PHP文件验证,php,mysql,validation,file-upload,if-statement,Php,Mysql,Validation,File Upload,If Statement,您好,我对php很陌生,但我一直在学习一些教程,但它们似乎不起作用,所以我尝试对它们进行调整。 我已经测试了这段代码,它在一定程度上可以正常工作,但还有一些事情我还没弄清楚,php文件没有上传(很好),但细节仍在写入数据库中,尽管$ok是spose设置为0(不好)。如果在这里解释将要发生的事情,可能会更容易: -用户可以上传gif或jpeg文件。详细信息添加到数据库中。 -用户不能上载任何文件,因为将使用默认设置。详细信息添加到数据库中。 -用户不能上传任何其他文件。数据库上不应有任何记录,用户

您好,我对php很陌生,但我一直在学习一些教程,但它们似乎不起作用,所以我尝试对它们进行调整。 我已经测试了这段代码,它在一定程度上可以正常工作,但还有一些事情我还没弄清楚,php文件没有上传(很好),但细节仍在写入数据库中,尽管$ok是spose设置为0(不好)。如果在这里解释将要发生的事情,可能会更容易:

-用户可以上传gif或jpeg文件。详细信息添加到数据库中。 -用户不能上载任何文件,因为将使用默认设置。详细信息添加到数据库中。 -用户不能上传任何其他文件。数据库上不应有任何记录,用户应重试

到目前为止,我的代码是:

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$ok=0; 


//This gets all the other information from the form
$name= mysql_real_escape_string ($_POST['nameMember']);
$bandMember= mysql_real_escape_string ($_POST['bandMember']);
$pic= mysql_real_escape_string ($_FILES['photo']['name']);
$about= mysql_real_escape_string ($_POST['aboutMember']);
$bands= mysql_real_escape_string ($_POST['otherBands']);

$uploaded_size=$_FILES['photo']['file_size']; 
if ($uploaded_size > 350000)
{
echo "Your file is too large, 35Kb is the largest file you can upload.<br>";
$ok=0;
} 
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
} 

if (!($uploaded_type =="image/jpeg"))
{
echo "JPEG<br>";$ok=1;
} 

if ($uploaded_type =="image/gif")
{
echo "GIf<br>";$ok=1;
} 

if (empty($pic)){
echo "You haven't uploaded a photo, a default will be used instead.<br/>";$ok=1;}


if ($ok==0)
{
Echo "Sorry your file was not uploaded, please try again with the correct format.";
}

//If everything is ok we try to upload it
else
{ 

// Connects to your Database
mysql_connect("localhost", "*******", "******") or die(mysql_error()) ;
mysql_select_db("project") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO dbProfile (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory<br/>";
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>";
}
else {

//Gives and error if its not
echo "<p>If you have uploaded a picture there may have been a problem uploading your file.</p>";
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>";
}
}
?> 

错误可能是以下if语句:

  if (!($uploaded_type =="image/jpeg"))
  {
    echo "JPEG<br>";$ok=1;
  }
if(!($upload\u type==“image/jpeg”))
{
echo“JPEG
”;$ok=1; }
因为每次上载内容类型不等于“image/jpeg”的图像时,$ok的计算结果为1,所以所有内容都会写入数据库

但也要注意,像这样检查MIME类型可能会给您带来麻烦,因为用户可以伪造文件的MIME类型

例如,您可以使用Imagick获得正确的图像MIME类型。请参阅此处的更多详细信息:


编辑:刚刚注意到,$U类型在脚本中的任何地方都不会初始化。正如我所说,您可以通过使用$_文件['photo']['type']粗略估计MIME类型。

那么$ok=0吗?让我直截了当地说,我现在说,如果文件不是jpeg,那么就让$ok=1;这实际上是错误的,我需要使它$ok=0;我将尝试更改它,看看这是否是问题所在。您是说我需要在php代码的顶部添加$u文件['photo']['type'],以检查文件的类型?