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...

Halid Kyazim
  • 25
  • 1
  • 8
  • 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. – pazof Apr 10 '17 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 }` – pazof Apr 10 '17 at 13:57
  • You could get the file extension using the pathinfo() function (http://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 http://stackoverflow.com/q/7575482/80836 – Andreas Apr 10 '17 at 13:59
  • pazof the resul is same thank you for answer :) – Halid Kyazim Apr 10 '17 at 14:06

0 Answers0