PHP file upload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Asprisa
    New Member
    • Oct 2007
    • 30

    PHP file upload

    Hi guys,

    I have managed to code a file that uploads a file, resizes it to 100 x 100 px, and then generates a random number and renames the image (so i do not get images over writing each other), what i am stuck on is trying to block uploads that are over 200kb, and if there is no file to upload (as there will be no need sometimes to upload a file) to skip the code all together in order to process some more code i got going on after this piece, can anyone help! i have looked around various websites and am just absoulty stuck on this!

    Any help is much appriciated

    The Code:

    <?php
    if(isset($_POST['submit'])){
    if (isset ($_FILES['file'])){
    imagename = $_FILES['file']['name'];
    $random_digit=r and(0000,9999);
    $new_file_name= $random_digit.$ imagename;

    $source = $_FILES['file']['tmp_name'];
    $target = "Articleima ges/".$new_file_nam e;
    move_uploaded_f ile($source, $target);


    $imagepath = $new_file_name;
    $save = "Articleima ges/" . $imagepath;
    $file = "Articleima ges/" . $imagepath;

    list($width, $height) = getimagesize($f ile) ;

    $modwidth = 100;

    $diff = $width / $modwidth;

    $modheight = 100;
    $tn = imagecreatetrue color($modwidth , $modheight) ;
    $image = imagecreatefrom jpeg($file) ;
    imagecopyresamp led($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

    imagejpeg($tn, $save, 100) ;

    $save = "Articleima ges/Thumbs/" . $imagepath;
    $file = "Articleima ges/" . $imagepath;

    list($width, $height) = getimagesize($f ile) ;

    $modwidth = 100;

    $diff = $width / $modwidth;

    $modheight = 100;
    $tn = imagecreatetrue color($modwidth , $modheight) ;
    $image = imagecreatefrom jpeg($file) ;
    imagecopyresamp led($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

    imagejpeg($tn, $save, 100) ;

    }
    }
    }
    ?>



    Cheers

    Jay!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You can obtain the size of the file in the $_FILES array.

    $_FILES['file']['size']

    Then use an IF check to compare sizes.

    Comment

    • nashruddin
      New Member
      • Jun 2008
      • 25

      #3
      move the code to resize and rename the image in a new function. something like this:

      Code:
      <?php
      /* function to resize and rename the uploaded image */
      function resize_and_rename($filename) {
        /* ...some code here...*/
      }
      
      /* process uploaded image */
      if (is_uploaded_file($_FILES['file']['tmp_name'])) {
        /* check if uploaded image size < 200kb */
        if ($_FILES['file']['size'] >= 200000) {
          die("Image size should be < 200kb!");
        } else {
          /* resize and rename the uploaded image */
          resize_and_rename($_FILES['file']['tmp_name']);
        }
      }
      ?>

      Comment

      • Asprisa
        New Member
        • Oct 2007
        • 30

        #4
        Hi Guys,

        Thank you very much for your replies, i managed to get the code working!

        Jay!

        Comment

        Working...