2017-02-10 61 views
0

我想上传一个.txt文件,并在w3schools中找到了这段代码。但是这个代码只允许图像上传并存储在本地驱动器中。如何将其更改为.txt文件上传格式?感谢您的回应。 :)用PHP上传文本文件

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<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> 
 

 
</body> 
 
</html>

这是PHP代码:

<?php 
     $target_dir = "uploads/"; 
     $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."; 
      } 
     } 
     ?> 
+1

如果($ imageFileType!= “JPG” && $ imageFileType!= “PNG” && $ imageFileType!= “JPEG” && $ imageFileType!= “GIF”) 你可以只添加的txt等扩展上面的代码 – fizzi

+0

为什么不做一些努力来搜索它。如果遇到任何特定的问题,然后张贴它。 – WeAreRight

回答

1

因为你只允许JPG, PNG, JPEG图片上传。只上传txt文件PLZ使用此代码

if($imageFileType != "txt") { 
      echo "Sorry, only txt files are allowed."; 
      $uploadOk = 0; 
     } 
+0

但我只想限制它只有.txt格式。不是所有的文件格式。 –

+0

然后,我已经为你代码 – shyamm

+0

只是检查它! – shyamm

0

在你的代码

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

正在检查的文件类型,如果它是一个图像或函数不会和getimagesize()。如果它不是你的图片$ uploadOk = 0;。通过这样做你的文本文件将总是给$ uploadOk = 0;。由于这种accortding的代码你的最后一部分:

// 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."; 
     } 
    } 

你将永远无法上传文本文件。因此,无论你改变你的代码和getimagesize()或干脆反向$ uploadOk的值在代码的第一部分是这样的:

// 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 = 0; 
     } else { 
      echo "File is not an image."; 
      $uploadOk = 1; 
     } 
    } 
0

你的文件应该是这样的所以要能够上传txt文件,检查文件类型“txt”。

<?php 
    $target_dir = "**the path u like**"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
    } 
    // 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 != "txt") { 
     echo "Sorry, only txt 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."; 
     } 
    } 
    ?>