Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Upload File Validation
    primarykey
    data
    text
    <p>I am creating file upload script and I'm looking for the best techniques and practices to validate uploaded files.</p> <p>Allowed extensions are:</p> <pre><code>$allowed_extensions = array('gif','jpg','png','swf','doc','docx','pdf','zip','rar','rtf','psd'); </code></pre> <p>Here's the list of what I'm doing.</p> <ol> <li><p>Checking file extension</p> <pre><code>$path_info = pathinfo($filename); if( !in_array($path_info['extension'], $allowed_extensions) ) { die('File #'.$i.': Incorrent file extension.'); } </code></pre></li> <li><p>Checking file mime type</p> <pre><code>$allowed_mimes = array('image/jpeg','image/png','image/gif','text/richtext','multipart/x-zip','application/x-shockwave-flash','application/msword','application/pdf','application/x-rar-compressed','image/vnd.adobe.photoshop'); if( !in_array(finfo_file($finfo, $file), $allowed_mimes) ) { die('File #'.$i.': Incorrent mime type.'); } </code></pre></li> <li><p>Checking file size.</p></li> </ol> <p>What should I do to make sure uploaded files are valid files? I noticed strange thing. I changed .jpg file extension to .zip and... it was uploaded. I thought it will have incorrect MIME type but after that I noticed I'm not checking for a specific type but if a specific MIME type exist in array. I'll fix it later, that presents no problems for me (of course if you got any good solution/idea, do not hesitate to share it, please).</p> <p>I know what to do with images (try to resize, rotate, crop, etc.), but have no idea how to validate other extensions.</p> <p>Now's time for my questions.</p> <ol> <li>Do you know good techniques to validate such files? Maybe I should unpack archives for .zip/.rar files, but what about documents (doc, pdf)?</li> <li>Will rotating, resizing work for .psd files?</li> <li>Basically I thought that .psd file has following mime: application/octet-stream but when </li> </ol> <p>I tried to upload .psd file it showed me (image/vnd.adobe.photoshop). I'm a bit confused about this. Do files always have the same MIME type?</p> <p>Also, I cannot force code block to work. Does anyone have a guess as to why?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload