Page 1 of 1

Uploaded files arrive in wrong order

Posted: Thu Sep 11, 2008 3:44 am
by mikeym
Hi,

I'm trying to sort out my site's image upload and I'm finding that the images are appearing in the wrong order.

Here's the order I select them.

Image

Here's my processing code:

Code: Select all

 
    global $database, $session;
    //set_time_limit(900);
    define ("JPG", 2);
    define ("PNG", 3);
    $folder=getBasicFilename($referer);
    if(!file_exists("../uploads/$folder")) {
        mkdir("../uploads/$folder");
    }
    if(!file_exists("../uploads/$folder/small")) {
        mkdir("../uploads/$folder/small");
    }
    $output="";
    $errors=0;
    $i=0;
    foreach($_FILES as $file) {
        $i=$i+1;
        if($file["error"] == UPLOAD_ERR_OK) {
            $filename=$file["name"];
            $size=$file["size"];
            $type=$file["type"];
            $tmpname = $file["tmp_name"];
            $filecount=$database->filecount($user);
            if (is_uploaded_file($tmpname)) {
                //Check file size under 2Mb
                $maxsize=2;
                //echo "Size: $size > $maxsize*1024*1024";
                if($size <= $maxsize*1048576){
                    $pext = getFileExtension($filename);
                    $pext = strtolower($pext);
                    if ($pext == "jpg" || $pext == "jpeg" || $pext == "png")
                    {
                        $width = 0;
                        $height = 0;
                        $type = "";
                        $attr = "";
                        list($width, $height, $type, $attr) = @getimagesize($tmpname);
                        // Double check the file type - don't trust the extention
                        if ($type==JPG || $type==PNG )
                        {
                            $newname="$user".sprintf("%06d", $filecount).".$pext";
                            $destination="../uploads/incomplete/$newname";  
                            if (!copy($tmpname, $destination)) 
                            {
                                $uploadform->setError("file_$i", "* Error uploading file $filename");
                            } else {    
                                chmod($destination, 0400);                                                  
                                $res = $database->query("INSERT INTO newimages SET image='$newname', name='$user', page='$referer', filenumber='$filecount', date=now() ");
                            }
                        } else {
                            //echo "Bad file type";
                            $uploadform->setError("file_$i", "* Error uploading file $filename. Bad file type.");
                        }
                    } else {
                        //echo "Bad file type";
                        $uploadform->setError("file_$i", "* Error uploading file $filename. Bad file type.");
                    }
                } else {
                    //echo "File to large - maximim is ".$maxsize."Mb.";
                    $uploadform->setError("file_$i", "* Error uploading file $filename. File to large - maximim is ".$maxsize."Mb.");
                }
            } else {
                //echo "Error uploading file $filename";
                $uploadform->setError("file_$i", "* Error uploading file $filename");
            }
            //Delete the temporary file
            unlink($tmpname);
        }
    }
    //set_time_limit(30);
    header("Location: ".$referer);
 
When I display them I order them from the database by date and the file names go out of order (and count backwards but that doesn't really matter), and the images are out of sequence.

Here is the results I get:

http://www.getoutofglasgow.com/Trip29.htm

Hopefully you can explain what's going on because I can't make heads nor tails of it.

Re: Uploaded files arrive in wrong order

Posted: Thu Sep 11, 2008 4:44 am
by mikeym
OK the problem appears to be not that the files are arriving in the wrong order but that the date field in my (mySQL) database isn't sensitive enough to note the difference between files arriving during the same second. The data is stored as a datetime (Y-m-d H:i:s).

Any suggestions as to what to store it as instead and how?

Re: Uploaded files arrive in wrong order

Posted: Thu Sep 11, 2008 8:28 am
by mikeym
Never mind! I've sorted it.