+ Reply to Thread
Results 1 to 3 of 3

Thread: Picture Uploading

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Picture Uploading

    I am trying to create a section where users will be able to make profiles on the website. This is so people can leave comments and stuff on emails and news articles and people will be able to learn a little about them. Also it will come in handy later when I start adding more and more features. I am having an issue though. I want people to be able to upload pictures to their profile. They would upload the picture, the server would make sure its a picture then email me telling me a picture is uploaded and waiting for approval. I am running into a few issues though.
    Anyway, when I try to upload a test picture it tells me that it is not jpeg or gif format, when it is. Thanks for the help in advance!

    Here is my code (its not all of the code, just the parts that are necessary):

    Code:
    <form method="post" action="editprofile.php" enctype="multipart/form-data">
    Change Picture<input type="hidden" name="MAX_FILE_SIZE" value="500" /><input type="file" name="file_up" />
    <br /></center>
    <input name="submit" type="submit" value="submit" /></center>
    </form>
    
        $file_upload="true";
        $file_up_size=$_FILES['file_up'][size];
        echo $_FILES['file_up'][name];
        if ($_FILES['file_up'][size]>150000)
            {
                $msg=$msg."Your uploaded file size is more than 150KB so please reduce the file size and then upload.<BR>";
                $file_upload="false";
            }
    
        if (!($_FILES['file_up'][type] =="image/jpeg" OR $_FILES['file_up'][type] =="image/gif"))
            {
                $msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
                $file_upload="false";
            }
    
        $file_name=$_FILES['file_up'][name];
        $add="images/pending/$file_name"; // the path with the file name where the file will be stored, upload is the directory name.
        if($file_upload=="true")
            {
    
            if(move_uploaded_file ($_FILES['file_up'][tmp_name], $add))
                {}
            else
                {
                    echo "Failed to upload file Contact Site admin to fix the problem";
                }
            }
        else
            {
                echo $msg;
            }

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Picture Uploading

    Debugging 101.


    PHP Code:
     
        
    if (!($_FILES['file_up'][type] =="image/jpeg" OR $_FILES['file_up'][type] =="image/gif"))
            {
               
    ## FIRST DEBUGGING CODE ADDED
     
               
    $msg .= "<p>The reported file type is:  " .  $_FILES['file_up'][type]  . "<br/>" ;
     
               
    ## END FIRST DEBUGGING CODE
     
     
                
    $msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
                
    $file_upload="false";
            } 
    This will print "The reported file type is :" , ie $_FILES['file_up'][type] is BLANK.

    Why? Further investigation...Check the entire $_FILES array

    PHP Code:
     
        
    if (!($_FILES['file_up'][type] =="image/jpeg" OR $_FILES['file_up'][type] =="image/gif"))
            {
               
    ## SECOND  DEBUGGING CODE ADDED
     
                
    foreach( $_FILES['file_up' ] as $k => $v ){
                   
    $msg .= "$k => $v <br />" ; }
     
               
    ## END SECOND  DEBUGGING CODE
     
                
    $msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
                
    $file_upload="false";
            } 
    This gives :

    Code:
     
    jpgname => Chopin.jpg 
    type => 
    tmp_name => 
    error => 2 
    size => 0
    What is error code 2? (0 is normally 'OK'). Go to Google and find...

    Code:
    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
    Aha! File too big. How big is too big?

    HTML Code:
     
    <input type="hidden" name="MAX_FILE_SIZE" value="500" /><
    500?

    Edit/Add...

    Your test

    PHP Code:
        if (!($_FILES['file_up'][type] =="image/jpeg" OR 
                
    $_FILES['file_up'][type] =="image/gif")) 
    isn't too good either. The type on jpeg is often ( ie, IE) reported as "image/pjpeg". Better to test for extensions or test the type with a regular expression.
    Last edited by descalzo; 11-13-2009 at 04:56 PM.

  3. #3
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: Picture Uploading

    ah ok, thanks i fixed it. i just took out the hidden html size restriction and changed the mime check, it works now, thanks!

+ Reply to Thread

Similar Threads

  1. convert picture to line art like a picture on comic cartoon
    By cafenet in forum Graphics & Webdesign
    Replies: 2
    Last Post: 05-15-2009, 09:59 AM
  2. Help with outputing a picture and text below it
    By purpleflame in forum Programming Help
    Replies: 2
    Last Post: 02-07-2009, 02:17 PM
  3. Slow uploading through Perl/CGI
    By oscetips in forum Programming Help
    Replies: 0
    Last Post: 06-06-2008, 12:53 PM
  4. How to protect picture from downloading from your site???
    By Goran in forum Scripts & 3rd Party Apps
    Replies: 24
    Last Post: 05-11-2008, 12:38 AM
  5. Earn 300 credits for find the text in the picture.
    By medphoenix in forum The Marketplace
    Replies: 19
    Last Post: 02-12-2008, 12:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers