SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload not uploading all pic types

    Hi Guys,

    i'm having trouble with my completed upload code

    PHP Code:
    <?php      
      $photo1 
    $_FILES["image1"]["name"];
      
    $username mysql_real_escape_string($_POST["username"]);
      
    $max_image_size 512000
      
    $allowed = array("image/gif","image/jpg","image/x-png","image/jpeg");
      
    $max_image_width 500;
      
    $max_image_height 450;
      
    $random_number rand(000000,999999);
      
       
    // Query for a previous photo...///////////////////////////////////////////////////////
       
    $result mysql_query("SELECT photo1 FROM membership WHERE username='".$username."' LIMIT 1");
       
    $row mysql_fetch_object($result);
       
    $photo_to_delete $row->photo1;
         
    unlink("uploads/".$photo_to_delete); 
      
      
    // The renamed photo...////////////////////////////////////////////////////////////////
      
    $renamed_photo $random_number.".".substr($_FILES["image1"]["name"],strlen($_FILES["image1"]["name"])-3,3); 
      
      
    // Check there wasn't a blank submission...////////////////////////////////////////////
      
    if(empty($_FILES["image1"]["name"])) {
      
           echo 
    "<p>Sorry, But No File Was Selected, Please Go Back And Try Again.</p>";
           exit;  
      }
      
      
    // Validation for Image...
      
    if ($_FILES["image1"]["size"] > $max_image_size) {
      
       echo 
    "<p>Sorry <b>$username</b> That Image Is Bigger Than The Allowed Size Of 3mb Please <a href=\"java script: history.go(-1)\">Go Back</a></p>";
       exit;
       
      }
      
      
    #####################################################################
      // Validate image dimensions...////////////////////////////////////////////////////////
      
    $dim getimagesize($_FILES["image1"]["tmp_name"]);

      if(
    $dim[0] >= $max_image_width || $dim[1] >= $max_image_height) {
      
        echo 
    "<p>Sorry, That Image Isn't Within The Current Upload Dimensions Please Go Back And Upload Another!</p>";
        exit;
        
      }  
      
    // Validate image types.../////////////////////////////////////////////////////////////
      
    if (in_array($_FILES["image1"]["type"], $allowed)) {
      
        echo 
    "<p>Sorry, That File Isn't One Of The Allowed File Types Only <b>.jpg</b> Are Allowed.</p>";
        exit;
        
      }  
      
    #####################################################################
      
      
    $uploadpath "uploads/"// <- Upload folder...
      
    $uploadpath $uploadpath.$renamed_photo;

      
    #####################################################################  
      
    if (!move_uploaded_file($_FILES["image1"]["tmp_name"], $uploadpath)) 
      
         die(
    "<p>Sorry, There Was An Error Uploading Your Image!");
         echo 
    "<p><br />The Image (<b><font color=\"red\">" .$_FILES["image1"]["name"]. "</b></font>) Has Been Uploaded Successfully!<br />";
         echo 
    '<br /><p>Please Wait To Be Re-Directed...</p>'
         echo 
    "<meta http-equiv=\"refresh\" content=\"3;URL=javascript: history.go(-1)\">";
         
        
      
    // Create our query.../////////////////////////////////////////////////////////////////
      
    $sql "UPDATE membership SET photo1='$renamed_photo' WHERE username='$username'";

      
    // Run our query...////////////////////////////////////////////////////////////////////
      
    $rs mysql_query($sql$conn) or die(mysql_error());
    ?>
    the problem is this code uploads SOME .jpg's and doesn't others , and also doesn't upoad .gif's, But if i change this:

    PHP Code:
    if (in_array($_FILES["image1"]["type"], $allowed)) { 
    to

    PHP Code:
    if (!in_array($_FILES["image1"]["type"], $allowed)) { 
    it uploads gif but not .jpgs

    im totally stumped any help would be great

    Graham

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding "image/pjpeg" to your allowed array, so:
    PHP Code:
    $allowed = array("image/gif","image/jpg","image/x-png","image/jpeg","image/pjpeg"); 
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  3. #3
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just print the type of the uploaded image to determine its type and add it to $allowed then.

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI Jean,

    Thanks i'v added that to the allowed list.

    @ mike thanks for the help mike how would i best print out the uploaded type out?

    thanks guys

    Graham

  5. #5
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use a basic echo to print out the information.
    PHP Code:
    echo $_FILES["image1"]["type"]; 
    Just put that somewhere in the loop and it'll print out all the file types you are using on that particular attempt
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jeanco,

    fixed now although it did say a.jpg was a image/gif lol

    cheers

    Graham


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
  •