PHP中的文件上载错误

PHP中的文件上载错误,php,Php,我正在使用PHP构建一个web应用程序。我正在其中创建一个文件上载部分,但我遇到了问题 <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Ima

我正在使用PHP构建一个web应用程序。我正在其中创建一个文件上载部分,但我遇到了问题

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

选择要上载的图像:
upload.php:

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["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"] > 500000) {
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.";
  }
  }
  ?>


对于图像来说,它工作得很好。但当我尝试检查上传mp4文件时会发生什么情况。it echo文件已存在,抱歉,只允许使用JPG、JPEG、PNG和GIF文件。抱歉,上载文件时出错。请指导我哪里做错了,这样我的工作就不会出错。提前感谢。

因为您有不允许其他扩展的条件,所以它根据代码工作:

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=    "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
显然你的目标文件夹中已经有这样的文件了。再检查一遍

对于图像来说,它工作得很好。但是当我试图检查如果 我上传mp4文件。很抱歉,只允许使用JPG、JPEG、PNG和GIF文件

这是一个限制,因为您的代码对其进行了限制

// 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;
}
如果文件不是
jpg
png
jpeg
gif
文件,则不允许上载

它回显已存在的文件

这仅仅是因为该文件已经存在(无论目录位于何处),由于该检查:

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
很抱歉,上载文件时出错

我不明白这是为什么。如果($uploadOk==0)在所有其他条件中设置为0,则条件
,因此应为true,并应回显“对不起,您的文件未上载”

移动到要询问的评论

HTML代码

单击“提交”按钮后,首先从本地选择文件 它重定向到upload.php文件,第一个函数是白色的,用于查找文件的扩展名,并返回您的文件扩展名 您需要创建一个文件夹(您可以在其中存储名称) 在扩展代码之后,它将获得带有日期和时间的文件名,您可以设置保存图像的文件的目标路径,然后运行插入查询。 首先检查文件夹权限,您可以在其中移动文件

谢谢
Vivek检查文件扩展名大小写。并允许上下两种情况


如果($imageFileType!=“jpg”&&&$imageFileType!=“png”&&&$imageFileType!=“jpeg”&&&$imageFileType!=“gif”&&&$imageFileType!=“jpg”&&&$imageFileType!=“png”&&$imageFileType!=“gif”)

我知道我已经限制了这段代码,但它的回音文件已经存在,很抱歉,只允许。我只想有错误,而不是文件已经存在+错误你对文件使用相同的名称吗?这是因为对你的php有限制file@AmitThakur它还显示文件已经存在。Y?你确定你上传的文件在目录中不存在吗?是的,请复制此代码并检查你的本地主机。Thanksit对我来说很好,经过一些修改==>&James我知道我已经限制了这段代码,但它的echo文件已经存在,很抱歉,只允许。我只想在代码中此注释上方显示error而不是file ready exits+error:
//检查$uploadOk是否因错误而设置为0
放置此项并告诉我结果:
echo$uploadOk;退出我收到此错误:文件是图像-图像/jpeg。抱歉,文件已存在。0我们已使用说明更新它
<form method="POST" enctype="multipart/form-data">
<input id="tags" type="file" name="employer_image"  />
<input type="submit" name="submit" value="submit">
</form
if (isset( $_POST['submit'] )
{
 function GetImageExtension($imagetype)
  {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }

      }
   if (!empty($_FILES["employer_image"]["name"])) {
   $file_name=$_FILES["employer_image"]["name"];
   $temp_name=$_FILES["employer_image"]["tmp_name"];
   $imgtype=$_FILES["employer_image"]["type"];
   $ext= GetImageExtension($imgtype);
   $imagename=date("d-m-Y").'-'.time().$ext;
   $target_path = "folder-path".$imagename;
  if(move_uploaded_file($temp_name, $target_path)) {
           /* write insert query for store in mysql*/
   }else{
   exit("Error While uploading image on the server");
   } 
  }
}