簡體   English   中英

在PHP中上傳文件給出錯誤

[英]File upload in PHP giving error

我正在使用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文件會發生什么。 它回顯文件已存在,對不起,只允許JPG,JPEG,PNG和GIF文件。過濾,上傳文件時出錯。 請指導我在哪里做錯誤,這樣我的工作對我沒有錯誤。 提前致謝。

它適用於圖像。 但是當我嘗試檢查如果我上傳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;
}

如果文件不是jpgpngjpeggif ,則不允許上傳。

它回聲文件已經存在

這只是因為該文件已存在(目錄所在的位置),由於此檢查:

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

抱歉,上傳文件時出錯

我不明白為什么會這樣。 在所有其他條件中,您的條件if ($uploadOk == 0)設置為0,因此應該為true並且應該回顯“抱歉,您的文件未上傳”。

轉到評論要問。

因為你有條件禁止其他擴展,所以它根據代碼工作:

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=    "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

顯然你已經在你的目標文件夾中有這樣的文件。 再次檢查。

HTML代碼

<form method="POST" enctype="multipart/form-data">
<input id="tags" type="file" name="employer_image"  />
<input type="submit" name="submit" value="submit">
</form

upload.php的

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");
   } 
  }
}

首先選擇文件形式你的本地后點擊提交按鈕它重定向到upload.php文件有第一個功能是白色,找到文件的擴展名,它返回你的文件擴展名,你需要創建一個文件夾(你可以存儲名稱)在擴展代碼之后,它獲取帶有日期和時間的文件名,您可以設置保存圖像的文件的目標路徑,然后運行插入查詢。 首先,檢查文件夾權限,以便移動文件

謝謝Vivek

檢查文件擴展名案例。 並允許上下兩種情況。

if($ imageFileType!=“jpg”&& $ imageFileType!=“png”&& $ imageFileType!=“jpeg”&& $ imageFileType!=“gif”&& $ imageFileType!=“JPG”&& $ imageFileType!=“PNG”&& $ imageFileType!=“JPEG”&& $ imageFileType!=“GIF”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM