2016-09-20 3 views
-1

Ich habe eine Seite, die ich möchte, dass Benutzer in der Lage sein, .pdf-, .docx- und .doc-Dateien hochzuladen. Die PDF- und DOCX-Uploads funktionieren einwandfrei, es werden jedoch keine DOC-Dateien zugelassen.PHP .doc Datei-Upload funktioniert nicht

Hier ist meine Form und Eingabe-Taste:

<form action="?action=uploadForm" method="post" enctype="multipart/form-data" style="border:1px solid black;"> 
<input type="file" class="btn btn-md btn-primary btn-block" name="fileToUpload" id="fileToUpload" accept=".pdf,.docx,.doc" title="PDF and Word files only" /> 

Es scheint, mit einer DOC-Datei, die die Datei nie in $_FILES gestellt wird.

Dies ist alles meine Dateiüberprüfung/Upload-Logik:

if (isset($_GET['action']) == 'uploadForm') { 
    // Get the option from the dropdown, so we can upload the form to the right directory 
    $selectedTypeOfForm = $_POST['typeOfForm']; 

    $target_dir = "files/" . $selectedTypeOfForm . "/";    // The directory for the upload to go to 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // The name of the file being uploaded 
    $fileName = basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
    $fileType = pathinfo($target_file,PATHINFO_EXTENSION); 

    dump($_FILES); 

    $_SESSION['message'] .= "<br>"; 

    // Check if file already exists 
    if (file_exists($target_file)) { 
     $_SESSION['message'] .= "Sorry, the file <b>" . $fileName . "</b> already exists.<br>"; 
     $uploadOk = 0; 
    } 

    // Check file size 
    if (($fileType == "pdf" && $_FILES["fileToUpload"]["size"] > MAXIMUM_PDF_SIZE) || ($fileType == "doc" && $_FILES["fileToUpload"]["size"] > MAXIMUM_WORD_SIZE) || ($fileType == "docx" && $_FILES["fileToUpload"]["size"] > MAXIMUM_WORD_SIZE)) { 
     $_SESSION['message'] .= "Sorry, the file <b>" . $fileName . "</b> is too large. The file must be under "; 

     if ($fileType == "pdf") { 
      $_SESSION['message'] .= (MAXIMUM_PDF_SIZE/1000000) . "MB for a PDF.<br>"; 
     } else { 
      $_SESSION['message'] .= (MAXIMUM_WORD_SIZE/1000000) . "MB for a Word document.<br>"; 
     } 

     $uploadOk = 0; 
    } 

    // Allow certain file formats 
    if($fileType != "pdf" && $fileType != "doc" && $fileType != "docx") { 
     $_SESSION['message'] .= "Sorry, only PDF, DOC, and DOCX files are allowed. You tried to upload a <b>" . strtoupper($fileType) . "</b>.<br>"; 
     $uploadOk = 0; 
    } 

    // FINAL UPLOAD 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
     $_SESSION['message'] .= "<br>Sorry, your file <b>" . $fileName . "</b> was not uploaded.<br>"; 
    // if everything is ok, try to upload file 
    } else { 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
      $_SESSION['message'] .= "<br>The file <b>" . basename($_FILES["fileToUpload"]["name"]) . "</b> has been uploaded.<br>"; 
     } else { 
      $_SESSION['message'] .= "<br>Sorry, there was an error uploading the file <b>" . $fileName . "</b>.<br>"; 
     } 
    } 

    // header('Location:/forms.php'); 
    echo "<a href='/forms.php'>FORMS</a>"; 
    exit; 
} 

Und eine .doc-Datei hochladen zeigt folgende Meldungen:

Sorry, the file already exists. 
Sorry, only PDF, DOC, and DOCX files are allowed. You tried to upload a . 

Sorry, your file was not uploaded. 
+0

Sie erhalten eine "die Datei existiert bereits"? Nun ... – arkascha

+0

'if (isset ($ _ GET ['Aktion']) == 'uploadForm')' - Zu viele Leute machen den gleichen Fehler, immer und immer wieder. Das ist ein falsches Positiv. –

+0

und warum wurde dies als "Javascript" markiert? –

Antwort

0

Das Problem mit upload_max_filesize und post_max_size in php war. Ini. Die Datei wurde entfernt, bevor mein Code sie jemals berührt hat, da diese Werte unter der Größe der .doc-Dateien lagen, die ich hochladen wollte.

Verwandte Themen