Picture Uploading

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
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;
        }
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Debugging 101.


PHP:
    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:
    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 [I]MAX_FILE_SIZE[/I] directive that was specified in the HTML form.

Aha! File too big. How big is too big?

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

500?

Edit/Add...

Your test

PHP:
    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:

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
ah ok, thanks i fixed it. i just took out the hidden html size restriction and changed the mime check, it works now, thanks!
 
Top