Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Same Error?

  1. #1
    Join Date
    Mar 2009
    Location
    Western Australia
    Posts
    386
    Tokens
    0

    Default Same Error?

    My code below is checking the file but for some reason the file extension keeps being report wrong, not in the array of allowed extensions so I continuously see the error "Only JPEG, JPG, GIF and PNG files are allowed."

    PHP Code:
    <?php
    if (isset($_SESSION['login'])) {
    backend_links();
    $button = &$_POST['upload_button'];
    $name = &$_POST['name'];

    if (isset(
    $button)) {

    if (empty(
    $name))
    echo 
    "<script type='text/javascript'>
    $(function() {upload_error('All fields required')});
    </script>"
    ;
    else {
     
    $file_name = &$_FILES['file']['name'];
     
    $file_temp = &$_FILES['file']['tmp_name'];
     
    $file_size = &$_FILES['file']['size'];
     
    $file_explode explode('.'$file_name);
     
    $file_ext strtolower(end($file_explode));
     
    $allowed = array('jpeg''jpg''gif''png');

     if (!
    in_array($file_ext$allowed)) {
     echo 
    "<script type='text/javascript'>
          $(function() {upload_error('Only JPEG, JPG, GIF and PNG files are allowed.')});
     </script>"
    ;
     }

     elseif
      (
    $file_size 2) {
     echo 
    "<script type='text/javascript'>
        $(function {upload_error('File must be 2MB or Smaller!')});
     </script>"
    ;
    }
    else {
     echo 
    "mamama";
    }

    }
    }
     
    ?>
          <br /><center><form action='home' method='post' encrypt='multipart/form-data'>
                 Image Name<br /><input type='text' name='name' autocomplete='off'> <br />
                 File<div id='file_upload'><span id='path'></span><input id='file' type='file' name='file'></div>
                 <input type='submit' value='Upload' name='upload_button'>
          </form><center>
     <?php

    }
    else 
    header('Location: login');
    ?>

  2. #2
    Join Date
    Jan 2011
    Location
    England, United Kingdom
    Posts
    253
    Tokens
    40
    Habbo
    Pegle

    Latest Awards:

    Default

    Maybe try adding the fullstop before the file extension, i'm no expert but it just seems to me like that could be the problem. If it's not, sorry I can't help you.


  3. #3
    Join Date
    Mar 2009
    Location
    Western Australia
    Posts
    386
    Tokens
    0

    Default

    That wouldn't work because the full stop is used to break say "image.png" into "image" and "png" and then end(); gets "png" as I understand, then I'm comparing that with the allowed extensions.

  4. #4
    Join Date
    Dec 2007
    Location
    Manchester
    Posts
    2,236
    Tokens
    118
    Habbo
    hamheyelliot

    Latest Awards:

    Default

    I know absolutely nothing about PHP, so I'm basically just reading this in a sort of linear-ish fashion.

    You establish the allowed extensions here: $allowed = array('jpeg', 'jpg', 'gif', 'png');

    However the code directly below it seems to say: "If the file extension falls into the category 'allowed', display the message "Only such and such are allowed" and rejects the file.

    I'm assuming the else { echo "mamama"; } is the successful outcome, so perhaps you should switch this with the one I mentioned before? Or replace the allowed file types with disallowed file types?

    I've got no clue how to express it in PHP so I hope at least some of it helps

  5. #5
    Join Date
    Mar 2009
    Location
    Western Australia
    Posts
    386
    Tokens
    0

    Default

    You're sort of right there but the "!" represents "NOT" so "if the extension doesn't fall into the category 'allowed' " then echo my error otherwise it would echo "mamama".
    Thank you for trying but I was told on PHPAcademy that the error was within my HTML form, encrypt is meant to be encrype
    Last edited by Shibby-Shabs; 22-07-2011 at 01:09 PM.

  6. #6
    Join Date
    Aug 2004
    Location
    United Kingdom
    Posts
    5,769
    Tokens
    1,249
    Habbo
    Beneficial

    Latest Awards:

    Default

    Is the extension of the file uppercase or lowercase? in_array is case-sensitive < Scrap that, I see a strtolower(); :]

    I'm having a look, if I DP sorry; I can't edit haha.
    Last edited by Homosexual; 22-07-2011 at 03:58 PM.
    what is fetch gretchen?

  7. #7
    Join Date
    Aug 2004
    Location
    United Kingdom
    Posts
    5,769
    Tokens
    1,249
    Habbo
    Beneficial

    Latest Awards:

    Default

    (omg, double post. can't edit above post!)

    This code alone works:
    PHP Code:
    <?php
    $file_name 
    "potato.PNG";
    $file_explode explode('.'$file_name);
    $file_ext strtolower(end($file_explode));
    $allowed = array('jpeg''jpg''gif''png');

    print_r($file_explode);
    print_r($file_ext);
    print_r($allowed);

    if (!
    in_array($file_ext$allowed)) {
        echo 
    'File extension not accepted.';
    } else {
        echo 
    'File extension right. Carry on.';
    }

    ?>
    So all I can suggest is that
    PHP Code:
    $file_name = &$_FILES['file']['name']; 
    don't catch the right filename, because when I set it myself, it worked :}
    what is fetch gretchen?

  8. #8
    Join Date
    Mar 2009
    Location
    Western Australia
    Posts
    386
    Tokens
    0

    Default

    That would work because you have a file name - the error in my code was that it couldn't get the file name because I messed up the form.

  9. #9
    Join Date
    Aug 2004
    Location
    United Kingdom
    Posts
    5,769
    Tokens
    1,249
    Habbo
    Beneficial

    Latest Awards:

    Default

    I'm such a fail for not reading your post before. Sorry for wasting your time
    what is fetch gretchen?

  10. #10
    Join Date
    Jul 2011
    Posts
    11
    Tokens
    0

    Default

    PHP Code:
    <?php 
    if (isset($button)) {
     
    $file_name = &$_FILES['file']['name']; 
     
    $file_explode explode('.'$file_name); 
     
    $file_ext strtolower(end($file_explode)); 
     
    var_dump($file_ext);
     echo 
    '<br/>';
     
    var_dump($file_name);
     echo 
    '<br/>';
     
    var_dump($file_explode);
    }
    What's it returning?
    Last edited by 0x00; 23-07-2011 at 02:22 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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