-1

Hi following coding i find in online for pdf file upload. But i need it for upload doc and docx file also with rename file. Following are the code.

<?php
    $pdfPath = "docmument_upload/";
    $maxSize = 102400000000;
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['upload_pdf'])) {   
        if (is_uploaded_file($_FILES['filepdf']['tmp_name'])) {
            if ($_FILES['filepdf']['type'] != "application/pdf") {
                echo '<p>This is not Docuent or pdf File</p>';
            } else if ($_FILES['filepdf']['size'] > $maxSize) {
                echo '<p class="error">To large file: ' . $maxSize . 'KB</p>';
            } else {
                $menuName = 'file.doc';
                $result = move_uploaded_file($_FILES['filepdf']['tmp_name'], $pdfPath . $menuName);
                if ($result == 1) {
                    echo '<p class="error">Uploaded</p>';
                } else {
                    echo '<p class="error">Uploading fail</p>';
                }
            }
        }
    }
?>

How can i do this??

sanji
  • 121
  • 1
  • 14

1 Answers1

0

Check mime types on http://www.freeformatter.com/mime-types-list.html and modify line:

if ($_FILES['filepdf']['type'] != "application/pdf") {

to:

if ($_FILES['filepdf']['type'] != "application/pdf" OR $_FILES['filepdf']['type'] != "typename2" OR $_FILES['filepdf']['type'] != "typename3) {

where typename is Your selected mimetypes eg. "application/msword" for .doc extension.

Adam Bieńkowski
  • 585
  • 4
  • 21
  • Just a couple of things. 1) Personally I'd suggest using || instead of OR. Mixing ||, OR, && and AND can cause odd issues. 2) `!in_array()` would be cleaner imo. 3) I _think_ your logic is flawed. – Jonnix Sep 03 '15 at 09:53
  • For .doc id "application/msword" . And for .docx?? – sanji Sep 03 '15 at 10:20
  • if ($_FILES['filepdf']['type'] != "application/pdf" OR $_FILES['filepdf']['type'] != "application/msword" OR $_FILES['filepdf']['type'] != "application/vnd.openxmlformats-officedocument.wordprocessingml.document"){ is also not working. – sanji Sep 03 '15 at 10:26
  • try to upload a .docx file and start code with echo '
    '; var_dump($_FILES); and check mime type on the screen
    – Adam Bieńkowski Sep 04 '15 at 12:07