1

I want to upload files, which formats are mp4,webm,ogg :) But when i am clicking upload button, he is uploading every type file types :) And saving as the file formats like a ".file" and calling "Sucessfully uploaded" .. How to upload only mp4, web or ogg formats.. :) Here are my codes:

<main>

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file"></input>
    <input type="submit" value="Upload"></input>
    </form>


    <?php
$maxsize = 800000000;
$fileformat = substr(@$_FILES["file"]["name"],4,-4);
$filename = rand(0,99999999).$fileformat;
$filepath = "uploaded/".$filename;

if (@$_FILES["file"]["size"]>$maxsize) {
 echo "maximum upload size is 800MB";   
} else {
    $d = @$_FILES["file"]["type"];
    if ($d=="video/mp4" || $d=="video/webm" || $d="video/ogg"){
        if (is_uploaded_file(@$_FILES["file"]["tmp_name"])) {
            $move = move_uploaded_file($_FILES["file"]["tmp_name"],$filepath);
                if ($move) {echo "succesfully uploaded";}
                else {echo "upload error";}
        }
    } else {echo "Supported video formats are mp4, webm and ogg";}

}
?>

</main>

..dont have any sytax problems, only error is "Undefined index "file" this should be normal error becouse i didnt choised file...

4
  • You could always check the extension of the file ($fileformat in your case) - but please be aware that this is easily spoofable and is by no means a secure way to block unwanted files from your server. Commented Apr 10, 2017 at 13:51
  • Also you could check the mime type (using mime_content_type()) - but this is also not 100% secure: if(function_exists('mime_content_type') && !preg_match('/video\/(mp4|webm|ogg)$/mi', mime_content_type($filepath))){ //allow upload } Commented Apr 10, 2017 at 13:57
  • You could get the file extension using the pathinfo() function (php.net/manual/en/function.pathinfo.php) and on the client you could try to restrict the types of files accepted by the input element. see here for more stackoverflow.com/q/7575482/80836 Commented Apr 10, 2017 at 13:59
  • pazof the resul is same thank you for answer :) Commented Apr 10, 2017 at 14:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.