Click here to Skip to main content
15,887,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is how selected files show on mobile chrome browser Image - https://i.stack.imgur.com/Sz7fI.jpg .So the uploads tend to fail because it doesn't recognize the file extension. But when I use desktop or laptop to upload file the browser shows the filename along with it extension so the file uploaded successfully. My question is that How do I go about this do I need to make some sort of php codings in other for the script to recognize or validate the file without extension? Because I tried uploading my video on YouTube and I noticed it didn't show the file extension after selecting file but the file uploaded successfully. Someone save me please am stuck


What I have tried:

HERE IS MY HTML FILE

  if (isset($_SESSION['message']) && $_SESSION['message'])
  {
    printf('%s', $_SESSION['message']);
    unset($_SESSION['message']);
  }


<form method="POST" action="upload.php" enctype="multipart/form-data">
  <div>
    <span>Upload a File:</span>
    <input type="file" name="uploadedFile" />
  </div>

  <input type="submit" name="uploadBtn" value="Upload" />
</form>  


UPLOAD.PHP FILE

// message

$message = ' ';

if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload')

{

  if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === 

UPLOAD_ERR_OK)

{   

// get details of the uploaded file 

 $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];


    $fileName = $_FILES['uploadedFile']['name'];

    $fileSize = $_FILES['uploadedFile']['size'];

    $fileType = $_FILES['uploadedFile']['type'];

    $fileNameCmps = explode(".", $fileName);

    $fileExtension = strtolower(end($fileNameCmps));

    // sanitize file-name
    $newFileName = md5(time() . $fileName) . '.' . $fileExtension;

    // check if file has one of the following extensions
    $allowedfileExtensions = array('mp3', 'ogg', '3gp', 'acc', 'wma');

    if (in_array($fileExtension, $allowedfileExtensions))
    {
      // directory in which the uploaded file will be moved
      $uploadFileDir = 'songs/';
      $dest_path = $uploadFileDir . $newFileName;

      if(move_uploaded_file($fileTmpPath, $dest_path)) 
      {
        $message ='File is successfully uploaded.';
      }
      else 
      {
        $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
      }
    }
    else
    {
      $message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
    }
  }
  else
  {
    $message = 'There is some error in the file upload. Please check the following error.<br>';
    $message .= 'Error:' . $_FILES['uploadedFile']['error'];
  }
}
$_SESSION['message'] = $message;
header("Location: test.php");
Posted
Updated 12-Apr-20 2:59am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900