Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Saved from hackers variables in php script

  1. #11
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    You want to be looking at the actual type of the file, as opposed to its name. You can access this with $_FILES['uploaded_file']['type'], or in your case:

    PHP Code:
    foreach($_FILES as $file) {
      if(
    $file['type'] == 'image/jpeg')
        echo 
    'allow';
      else
        echo 
    'no';

    That shows you the basic concept, but just make an array like before, but this time with different MIME types, a list of which can be found here: http://www.webmaster-toolkit.com/mime-types.shtml

    So something like:

    PHP Code:
    $allowed = array("image/png""image/jpeg"); 
    Etcetera...

    You should be able to go from there with it. See how you get on.

  2. The Following User Says Thank You to Schmoopy For This Useful Post:

    auriaks (02-16-2010)

  3. #12
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Thanks... Good post from you

    Btw, can you solve my other need?

    I want prevent files' size. if ($size > 0.3mb) {not allow} else {allow}

  4. #13
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    This should do it. Also note that the max file size is also declared in the php.ini file, and is normally 2MB, so you'll need to edit that file as well if you want to upload files greater than 2MB.

    PHP Code:

    define
    ("MAX_FILE_SIZE"314573); // This is the file size in bytes

    foreach($_FILES as $file) {
      if(
    $file['size'] > MAX_FILE_SIZE) {
        echo 
    "The file you're trying to upload is too big";
      } else {
        
    // Code for uploading file here
      
    }

    314573 Bytes = ~0.3MB.

    An easy way to convert MB to bytes is to use: http://www.matisse.net/bitcalc/.

    Or if you don't like that one, just search for "bits to bytes converter" in Google.

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    auriaks (02-16-2010)

  6. #14
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    php.ini ? why should I do that?

  7. #15
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    You only need to change that if you're wanting to upload files greater than 2MB. But from the looks of it, you only want files that are 0.3MB or less, so no need to worry.

  8. #16
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    OK then... thanks Brilliant

  9. #17
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Have problem again, I am upoading right format, but script says that wrong...

    PHP Code:
    define("MAX_FILE_SIZE"307200); // This is the file size in bytes
    foreach($_FILES as $file) {
      if(
    $file['size'] > MAX_FILE_SIZE) {
        
    $error .= "<li>Nuotraukos dydis viršija 0.3mb!</li>";
      } else {
    foreach(
    $_FILES as $file) {
    $allowed = array('image/png''image/jpeg''image/bmp''image/gif''image/pjpeg');  
      if(
    $file['type'] == $allowed) {
    $target_path $target_path basename$_FILES['uploadedfile']['name']);  
    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {$fileName basename($_FILES['uploadedfile']['name']);}  } else 
    {
    $tt $file['type'];
    $error .= "<li>Nuotrauka yra neleistino $tt formato. Leistini: .png .jpg .gif .bmp</li>";}}  
    }} 

  10. #18
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Atm you're doing:

    PHP Code:
    if($file['type'] == $allowed
    So you're actually comparing it to the $allowed array as a whole, and not checking whether the file type is actually one of the values in the allowed array. Change your code to:

    PHP Code:
    define("MAX_FILE_SIZE"307200); // This is the file size in bytes

    $allowed = array('image/png''image/jpeg''image/bmp''image/gif''image/pjpeg');  

    foreach(
    $_FILES as $file) {
            if(
    $file['size'] > MAX_FILE_SIZE) {
                
    $error .= "<li>Nuotraukos dydis viršija 0.3mb!</li>";
            } else {
            if(
    in_array($file['type'], $allowed)) {
                
    $target_path $target_path basename$_FILES['uploadedfile']['name']);  
                if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
                
                    
    $fileName basename($_FILES['uploadedfile']['name']);}  
                    
                } else {
                    
    $tt $file['type'];
                    
    $error .= "<li>Nuotrauka yra neleistino $tt formato. Leistini: .png .jpg .gif .bmp</li>";
                }
        }

    Sorry about the formatting

  11. #19
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Better thanks again (I schould be sorry for that )
    Last edited by auriaks; 02-16-2010 at 08:03 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •