Search code examples
phpfile-upload

How to allow all file types for upload in php?


Hello I have a php script that is used to allow file uploads. The script has an array that with allowable file types.

      $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'xlsx')

Because the form is behind a log in area for my client, he is requesting to allow ALL file types, rather than to be restricted to files in the array. Although I have already explained to the client that it would be better to limit the files, he insist on having it open.

Is there a way to allow for any file type in an array?

Full Script

      $uploadDir = '/uploads/';

      // Set the allowed file extensions
      $fileTypes = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'xlsx', 'cad', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'odt', 'xls', 'xlsx', '.mp3', 'm4a', 'ogg', 'wav', 'mp4', 'm4v', 'mov', 'wmv' ); // Allowed file extensions

     $verifyToken = md5('unique_salt' . $_POST['timestamp']);

    if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile   = $_FILES['Filedata']['tmp_name'];
$uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
$targetFile = $uploadDir . $_FILES['Filedata']['name'];

// Validate the filetype
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array(strtolower($fileParts['extension']), $fileTypes)) {

    // Save the file
    move_uploaded_file($tempFile, $targetFile);
    echo 1;

  } else {

    // The file type wasn't allowed
    echo 'Invalid file type.';

  }
    }

Solution

  • Just don't do the filtering.

    <?php
    $uploadDir = '/uploads/';
    
    $verifyToken = md5('unique_salt' . $_POST['timestamp']);
    
    if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
        $tempFile   = $_FILES['Filedata']['tmp_name'];
        $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
        $targetFile = $uploadDir . $_FILES['Filedata']['name'];
    
        // Save the file
        move_uploaded_file($tempFile, $targetFile);
        echo 1;
    }