Search code examples
phpmime

PHP Accepting only one extension type to upload. The file is an unknown extension type


this my first question here. I am new to PHP, but I have been searching for an answer for this question for about an hour, and have had no luck. Maybe I am just no reading it correctly, but I hope you guys can help me. I am trying to let people only upload one file type to my website. That file type is a .osk . I am not sure if I am doing this correctly, but I have it set up like this

$types = array ('application/osk'); 

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$target_path = "skins/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path, $types)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

I get an error whenever I submit that specific file type. And I also get an error if I submit anything else, but that is expected. Sorry if the code is a mess. I am new to this as I said before. But, I would love it if someone could help me figure out this.


Solution

  • Try this code

    $file_parts = pathinfo($_FILES['uploadedfile']['name']);
    // Check file is osk
    if($file_parts['extension'] == 'osk')
    {
      // move file
      if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path))
      {
        echo "Uploaded";
      }
      else
      {
        echo "File was not uploaded";
      }
    }