Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    php file upload problem. need help

    It is supposed that when i upload a file greater than 100kb. i should get an error.. but the file still uploads..

    Here is the code:

    PHP Code:
    <?php
    if(isset($_FILES['file'])){
    $name $_FILES['file']['name']; 
    $type $_FILES['file']['type'];
    $size $_FILES['file']['size'];
    $max_size 100000;
    $extension strtolower(substr($namestrpos($name'.') + 1));






    $tmp_name $_FILES['file']['tmp_name'];

    if (isset(
    $name)) {
    if(!empty(
    $name)) { 

      if (
    $extension=='jpg'||$extension=='jpeg'&&$type=='image/jpeg'&&$size<=$max_size) {
       
    $location 'uploads/';
       
      if  (
    move_uploaded_file($tmp_name$location.$name)) {
        echo 
    'Uploaded!';
      
      }
      
      } else {
        echo 
    'File must be jpg or jpeg and 1mb';
      }
      

    } else {
    echo 
    'Please choose a file';
    }
    }
    }

    ?>
    <form action="page2.php" method="POST" enctype="multipart/form-data">
         <input type="file" name="file"><br /><br />
        <input type="submit" value="Submit">
    </form>
    Last edited by Inigoesdr; 09-20-2011 at 03:15 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Try to echo the file size before checking it.
    So can you see if the file you uploaded really is 100kb

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Please would you start using [php] tags? - See the link in my signature.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by tangoforce View Post
    Please would you start using [php] tags? - See the link in my signature.
    Yes indeed, This is very irritating to read for us.

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I already echo the file size it is over 100kb it is 185kb its still not working

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    i've a solution to it found the solution... its ok now

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Could you please paste teh solution here so we also know what was wrong.

  • #8
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    From a look i guess the problem was this if

    PHP Code:
    if ($extension=='jpg'||$extension=='jpeg'&&$type=='image/jpeg'&&$size<=$max_size
    I guess he changed it to something like

    PHP Code:
    if (($extension=='jpg'||$extension=='jpeg')&&$type=='image/jpeg'&&$size<=$max_size

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    I don't think that.
    That check: if ext = jpg OR jpeg AND ... AND ...

    So i don't think that what you say is the solution

  • #10
    New Coder
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I make it like this: if (isset($name)) {
    if(!empty($name)&&$extension=='jpg'||$extension=='jpeg'&&$type=='image/jpeg') {

    if ($size<=$max_size) {
    $location = 'uploads/';

    if (move_uploaded_file($tmp_name, $location.$name)) {
    echo 'Uploaded!';

    }

    } else {
    echo 'File must be jpg or jpeg and 1mb';
    }


  •  

    Posting Permissions

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