Friday 15 July 2011

html - PHP: File extension is empty when user uploads image with unallowed extension -


I have a form on my website that lets all users upload images to the website. HTML and PHP:

  & lt; Html & gt; & Lt; Body & gt; & Lt ;? Php if ($ _ SERVER ['REQUEST_METHOD'] == 'POST') {$ error = ''; // Checking other input fields ... // If any other is valid, try uploading the file (to avoid uploading unsuitable c ** p) If (isset ($ _ FILES ['file' ]) & $ Error = = '' & Amp; $ _FILES ['file']) {$ file = $ _FILES ['file']; $ File_name = $ file ['name']; $ File_tmp = $ file ['tmp_name']; $ File_size = $ file ['size']; $ File_error = ''; Resonance $ file $ file_name $ file_tmp $ FILE_SIZE; $ File_ext = pathinfo ($ file_name, PATHINFO_EXTENSION); If ($ file_ext! = 'Png' and $ file_ext! = 'Jpg' & amp; $ file_ext! = 'Jpeg' & amp; $ file_ext! = 'GIF' & amp; $ file_ext ! 'BMP') {$ file_error = "& Lt; br> Only PNG, JPG, JPEG, GIF and BMP formats are allowed!"; } If ($ file_size> 1048576) {// & lt; & Lt; 1 MB byte $ $ file_error = "& Lt; br> The maximum size is 1MB."; } If (empty ($ file_error)) {$ file_name_new = uniqid ('', true). '.' . $ File_ext; $ File_destination = 'IMG / USR /' $ File_name_new; If (rename ($ file_tmp, $ file_destination)) // // resonance '& lt; Br> . $ File_destination; } And {$ file_error. = '& Lt; Br> Something went wrong when uploading a file ';}} $ file_error echo; }}? & Gt; & Lt; Form method = "post" action = "" enctype = "multipart / form-data" & gt; & Lt; Fieldset & gt; & Lt; Story & gt; Form & lt; / Narrative & gt; & Lt ;! - Other input fields - & gt; & Lt; Div id = "form_element" & gt; Upload Image: & lt; / Div & gt; & Lt; Input type = "file" name = "file" & gt; & Lt; Span id = "error" & gt; & Lt; Small & gt; & Lt ;? Php resonance $ file_error; // resonance $ file '& lt; Br> . $ _FILES ['file']. '& Lt; Br> . $ File_name $ ['name'] file; // resonance '& lt; Br> . $ File_tmp '& lt; Br> . $ File ['tmp_name'] '& lt; Br> . $ File_ Size '& lt; Br> . $ ['Size'] file; ? & Gt; & Lt; / Small & gt; & Lt; / Span & gt; & Lt; Div id = "form_element" & gt; & Lt; Input type = "submit" value = "poster" id = "submit" & gt; & Lt; / Div & gt; & Lt; / Fieldset & gt; & Lt; / Form & gt;  

There are several problems when the user uploads a file that does not meet the requirements (size, format etc.), it has still been uploaded Although, $ error! = '' When the user does not fill the file input field at all, the code should not upload the file if statement (yes, I know, I do not know the correct word ...), but "file Upload something went wrong "is still displayed when the image is submitted in the rejected format, it still uploads but looks like path / url / folder / img. - There are no file extensions.

Try it ...

  & lt ;? Php IF (isset ($ _ POST ['submit'])) {$ err = ""; $ Mimetypes = array ('image / gif', 'image / jpeg', 'image / pjpeg', 'image / jpg', 'image / png', 'image / x-png'); $ Imgexts = array ('gif', 'jpeg', 'jpg', 'png'); If (empty ($ _ files ['file'] ['name'])) {$ img = $ _FILES ['file'] ['name']; $ Temp = $ _FILES ['File'] ['tmp_name']; $ Ext = strtolower (substr (strrchr ($ _ files ['file'] ['name'], '.'), 1)); // file ext $ Info = @getimagesize ($ _ files ['file'] ['tmp_name']); $ Imgw = $ info [0]; // width $ imgh = $ info [1]; // height $ mime = image_type_to_mime_type ($ info [2]); IF (! In_array ($ ext, $ imgexts)) {$ err. = "& Lt; li & gt; File type (" .ext. ") Is not supported. (Only" .implode (",", $ imgexts). ") "; // non-supported file type} ELSE {// IF (! In_array ($ mime, $ mimetypes)) {$ err. = "& Lt; li & gt; Mime type (". $. ") Is not supported. (Only" .implode (",", $ mimetypes). ") & Lt; / li & gt;"; // non-supported mime type}}} ELSE {$ err = "
  • Select a file to upload (" .implode (",", $ imgexts). ") & Lt; / Li & gt; "; } If (empty ($ mistake)) {echo '& lt; P & gt; Errors: & lt; / P & gt; & Lt; Ul & gt; ' $ Err '& Lt; / Ul & gt; '; } ELSE {// good go}} ELSE {// form not sumbitted}? & Gt;

  • No comments:

    Post a Comment