2017-02-10 7 views
0

Ich möchte eine TXT-Datei hochladen und ich fand diesen Code in w3schools. Mit diesem Code können jedoch nur Bilder hochgeladen und auf einem lokalen Laufwerk gespeichert werden. Wie ändere ich das in ein .txt-Datei-Upload-Format? Danke für die Antwort. :)Textdatei in PHP hochladen

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

Dies ist der PHP-Code:

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

if ($ imageFileType! = "Jpg" && $ imageFileType! = "Png" && $ imageFileType! = "Jpeg" && $ imageFileType! = "Gif") Sie können nur txt-Erweiterung hinzufügen, wie der oben genannte Code – fizzi

+0

Warum nicht etwas Mühe beim googeln machen. Wenn Sie auf ein bestimmtes Problem stoßen, dann posten Sie es. – WeAreRight

Antwort

1

Da Sie nur erlauben JPG, PNG, JPEG Bilder hochladen. nur txt zum Hochladen von Dateien plz verwenden diesen Code

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

Aber ich möchte es nur auf das .txt-Format beschränken. Nicht alle Dateiformate. –

+0

dann habe ich es für Sie in Code – shyamm

+0

getan, überprüfen Sie es einfach !! – shyamm

0

In Ihrem Code

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

Du bist gerade den Dateityp, wenn sie ein Bild oder nicht durch die Funktion getimagesize() ist. Wenn es kein Bild ist, machen Sie $ uploadOk = 0;. Auf diese Weise erhält Ihre Textdatei immer $ uploadOk = 0;. Aufgrund dieses accortding zum letzten Teil des Codes:

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

Sie werden nie eine Textdatei hochladen. Also entweder Sie Ihren Code in getimagesize() ändern oder einfach die Werte von $ UploadOK im ersten Teil des Codes umgekehrt zu wie folgt aussehen:

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

Ihre Datei so sein würde, so zu sein in der Lage, TXT-Dateien hochzuladen, prüfen Sie den Dateityp "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."; 
     } 
    } 
    ?>