PHP Upload question

Discussion in 'PHP' started by jrp1, May 1, 2009.

  1. #1
    I want my php upload script to redirect to a page if the file is too big. I tried this:

    if (!isset($_FILES['userfile'])) exit;
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    if ($_FILES['userfile']['size']>$max_size) 
    { 
    else 
    { 
    echo "<meta http-equiv=\"refresh\" content=\"0; url=REDIRECT.php\">;
    Code (markup):
    but it didn't work. Any ideas what I did wrong? Thanks.
     
    jrp1, May 1, 2009 IP
  2. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Tried changing
    if ($_FILES['userfile']['size']>$max_size)

    to

    if (filesize($_FILES['userfile']['size'])>$max_size)
     
    dannywwww, May 1, 2009 IP
  3. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Alright, just tried that, but for some reason it still doesn't work.

    Any other ideas?
     
    jrp1, May 1, 2009 IP
  4. renlok

    renlok Well-Known Member

    Messages:
    457
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #4
    that wont work $_FILES['userfile']['size'] is an integer value not a file

    idk but try removing
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    i don't know if its causing the problem but theres no point running this function
     
    renlok, May 1, 2009 IP
  5. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    The
    if (!isset($_FILES['userfile'])) exit;
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    if ($_FILES['userfile']['size']>$max_size)
    Code (markup):
    code is fine. It's what's after it; thats what's giving me errors, like "invalid syntax "else" on line blah blah. Is echoing a redirect the right move? Or should I make it a header? I don't know how to make it redirect if it exceeds $max_size.
     
    jrp1, May 1, 2009 IP
  6. yz4now

    yz4now Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I believe you should use something like this:

    if ($_FILES['userfile']['size']>$max_size){
    header("Location:error.php?type=filesize");
    }

    If you have already started to output your HTML before this part then you need to use:
    echo "<meta http-equiv=\"refresh\" content=\"0; url=REDIRECT.php\">;
    Then you need to add a "die;" so that if something goes wrong the script don't keep loading.

    Remember that If/Else statements are setup like

    if(){

    }else{

    }

    ]www.php.net/if

    Hope that helps.
     
    yz4now, May 1, 2009 IP
  7. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    I tried:
    
    if ($_FILES['userfile']['size']>$max_size){
    header("Location:errorpage.php");
    }
    Code (markup):
    but it takes me to a blank page. It doesn't redirect to the page I told it to.
     
    jrp1, May 1, 2009 IP
  8. yz4now

    yz4now Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    is errorpage.php a file on your server?
    What is the URL in the address bar when the page is done loading?
     
    yz4now, May 1, 2009 IP
  9. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    The URL of the original file. And yes, its on my server.
     
    jrp1, May 1, 2009 IP
  10. yz4now

    yz4now Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well try this:
    if ($_FILES['userfile']['size']>$max_size){
    echo $_FILES['userfile']['size].">".$max_size;
    die;
    }

    This will echo out the values and will verify that the file was to large. If the page comes up blank then that means that the file was not to big.

    Let me know what you get.
     
    yz4now, May 1, 2009 IP
  11. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #11
    Parse error: syntax error, unexpected T_STRING, expecting ']' in /home/[username]/public_html/engine.php on line 50
     
    jrp1, May 1, 2009 IP
  12. yz4now

    yz4now Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Opps sorry that was an error on my part. I was writing the code to faster here is the fixed version.

    if ($_FILES['userfile']['size']>$max_size){
    echo $_FILES['userfile']['size'].">".$max_size;
    die;
    }

    What is the value of $max_size?
     
    yz4now, May 1, 2009 IP
  13. jrp1

    jrp1 Active Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #13
    Blank page on the original file.

    $max_size = 2097152 (thats in bytes)
     
    jrp1, May 1, 2009 IP
  14. yz4now

    yz4now Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Okay so that means that the file was not bigger than 2097152 so you get a blank page. How big was the file that you uploaded?
     
    yz4now, May 1, 2009 IP
  15. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #15
    try
    
    if ($_FILES['userfile']['size']>$max_size){
    header("Location:errorpage.php");
    die();
    }
    else
    {
        //write here the code you need to run when the file is not bigger than $max_size
    }
    
    PHP:
    may be it is not bigger and because you just don't write code for processing not big files it shows a blank page :D

    of may be it is because the line
    if (!isset($_FILES['userfile'])) exit;
    PHP:
    if $_FILES['userfile'] is not existent it will die not showing any thing
    I recommend to make it show some thing like error message describing the error. at least in that case you will know which part of the code cause it to show a blank page
     
    nayes84, May 1, 2009 IP
  16. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #16
    use this

    ob_start();
    if (!isset($_FILES['userfile']) || !is_uploaded_file($_FILES['userfile']['tmp_name']) || $_FILES['userfile']['size']>$max_size )
    {
    header("location:somepage.php");
    exit();
    }
    else
    {
    ///do something
    }
     
    octalsystems, May 2, 2009 IP