Image uploading script, very very strange error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Image uploading script, very very strange error

Post by invisibled »

Hey Everybody,

this is such a weird and frustrating error. The code below is for a client's gallery. She chooses an image from a standard html form and then submits, witch triggers this function. The script re sizes and uploads the file to two folders on the server and at the bottom, the $query and mysql_query inserts the file name into a database. The problem is, is that when you run the script, the query fails every time, i can't figure out why, but it never executes successfully.The worst part is that it was working one day, and then the next it wasn't working, nobody touched the code or the database. Could anybody suggest anything, even if its a total "DUH".... it might help! haha thanks.

Code: Select all

if(isset($_POST['Submit'])):
        $size = 480; // the thumbnail height
 
        $filedir = '../../pictures-videos/images/'; // the directory for the original image
        $thumbdir = '../../pictures-videos/thumbs/'; // the directory for the thumbnail image
        $prefix = ''; // the prefix to be added to the original name
 
        $maxfile = '2000000';
        $mode = '0666';
 
        $userfile_name = $_FILES['image']['name'];
        $userfile_tmp = $_FILES['image']['tmp_name'];
        $userfile_size = $_FILES['image']['size'];
        $userfile_type = $_FILES['image']['type'];
 
        if (isset($_FILES['image']['name'])):
            $prod_img = $filedir.$userfile_name;
 
            $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
            move_uploaded_file($userfile_tmp, $prod_img);
            chmod ($prod_img, octdec($mode));
 
            $sizes = getimagesize($prod_img);
 
            $aspect_ratio = $sizes[1]/$sizes[0]; 
 
            if ($sizes[1] <= $size)
            {
                $new_width = $sizes[0];
                $new_height = $sizes[1];
            }else{
                $new_height = $size;
                $new_width = abs($new_height/$aspect_ratio);
            }
 
            $destimg=ImageCreateTrueColor($new_width,$new_height)
                or die('Problem In Creating image');
            $srcimg=ImageCreateFromJPEG($prod_img)
                or die('Problem In opening Source Image');
            if(function_exists('imagecopyresampled'))
            {
                imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
                or die('Problem In resizing');
            }else{
                Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
                or die('Problem In resizing');
            }
            ImageJPEG($destimg,$prod_img_thumb,90)
                or die('Problem In saving');
            imagedestroy($destimg);
        endif;
        
        $query = "INSERT INTO ida_gallery VALUES ('','$userfile_name','')";
        
        mysql_query($query) or die ($query." Failed");
 
    endif;
Sourabh
Forum Newbie
Posts: 7
Joined: Sat Sep 13, 2008 2:18 am

Re: Image uploading script, very very strange error

Post by Sourabh »

can you give the details of ur DB table structure it would be one of the reason :?:
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: Image uploading script, very very strange error

Post by invisibled »

entryID - is an auto intrementing primany key
file - the file name of the image, varchar 255
caption - a caption of the picture, varchar 255
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image uploading script, very very strange error

Post by onion2k »

Firstly, you aren't escaping any of the data you're inserting. Use mysql_real_escape_string().
Secondly, it's a lot better to define the column names in your INSERT.
Thirdly, you'll get more useful information from the die() if you add a call to mysql_error().
Lastly, you really ought to specify which database connection calls to things like mysql_query() use.

Once that lot is done you'll end up with something like...

Code: Select all

$userfile_name = mysql_real_escape_string($userfile_name, $database_connection);
$query = "INSERT INTO ida_gallery (`file`,`caption`) VALUES ('".$userfile_name."','')"; //Note we no longer need to have the id column
mysql_query($query, $database_connection) or die ($query." Failed - ".mysql_error($database_connection));
Post Reply