$_FILES problem with uploading

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

danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: $_FILES problem with uploading

Post by danielfs1 »

Actually we are not trying to save space as much as just being able to resize the picture before we put it into the database.

Anyways im not at work, but will the mysql_real_escape_string work with pear? I think thats why we were using addslashes.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: $_FILES problem with uploading

Post by jmut »

danielfs1 wrote:Actually we are not trying to save space as much as just being able to resize the picture before we put it into the database.

Anyways im not at work, but will the mysql_real_escape_string work with pear? I think thats why we were using addslashes.
nothing todo with pear. php.net/mysql_real_escape_string.
And addslashes is one function I see no use of. stripslashes() I use once (to handle magic_quotes)
danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: $_FILES problem with uploading

Post by danielfs1 »

Well the addslashes wasn't what is corrupting it.

Its this line
imagejpeg($NEW_IMAGE, $file, 85);

After commenting it out, the picture will show up but won't be resized.

Also I'm getting an error with this:
imagecopyresampled($NEW_IMAGE, $OLD_IMAGE, 0, 0, 0, 0, $NEW_WIDTH, $NEW_HEIGHT, $ORIGINAL_WIDTH, $ORIGINAL_HEIGHT);
imagecopyresampled(): supplied argument is not a valid Image resource in

Here is the code for the particular section:

Code: Select all

 
 $NEW_IMAGE = imagecreatetruecolor($NEW_WIDTH, $NEW_HEIGHT);
             $OLD_IMAGE = new_image_by_type($_FILES['binFile']['name'], $FILE_EXTENSION);
             imagecopyresampled($NEW_IMAGE, $OLD_IMAGE, 0, 0, 0, 0, $NEW_WIDTH, $NEW_HEIGHT, $ORIGINAL_WIDTH, $ORIGINAL_HEIGHT);
      
             // save image as a jpg and optimize the file size..
             imagejpeg($NEW_IMAGE, $file, 85);
             $content = file_get_contents($file);
             $content = addslashes($content);           
             $fileSize = filesize($file);
 
if you need more code you can find it a few post ago, nothing has changed.


by the way for some reason the mysql_real_escape_string doesn't work with pear
coolbung
Forum Newbie
Posts: 8
Joined: Mon Feb 18, 2008 9:37 am

Re: $_FILES problem with uploading

Post by coolbung »

On line#30

Code: Select all

echo move_uploaded_file($_FILES['binFile']['name'],"C:\\xampp\\tmp");
The move_uploaded_file function expects the second argument to be the destination file name and not the destination folder path. You should be using something like. Also the first argument is the tmp_name as already discussed in one of the posts.

Code: Select all

 
$dest = "C:\\xampp\\tmp\\".$_FILES['binFile']['name'];
echo move_uploaded_file($_FILES['binFile']['tmp_name'],$dest);
 
Assuming the above function returns true, you can then use $dest anywhere u need a path to the new file.
User avatar
zsines
Forum Newbie
Posts: 10
Joined: Tue Feb 12, 2008 4:55 pm
Location: Atlanta, GA

Re: $_FILES problem with uploading

Post by zsines »

by the way for some reason the mysql_real_escape_string doesn't work with pear
This function will not work because it is designed to work specifically with a MySQL database. You need to find something that will work with PEAR, such as prepare(). This should do the trick, but for some reason, it does not.

I don't know what else to say, other than if danilefs1 is using PEAR, mysql_real_escape_string will not work. It cannot be interpreted correctly and will throw an error on the page. It will only (and I mean ONLY) work if the entire page is in MySQL references, with out the use of any PEAR tags ($conn->query, fetchRow(), etc.).

This won't work either, because the site needs to function with different types of databases.

He can also not write to the server, due to write restrictions that the organizations using this software (Georgia Southern University to name one) has put into place. So saving to the database is the only option.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: $_FILES problem with uploading

Post by Benjamin »

danielfs1 wrote:Well the addslashes wasn't what is corrupting it.
Yes it was.
danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: $_FILES problem with uploading

Post by danielfs1 »

Ive taken it in and out and its not causing any problems its self.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: $_FILES problem with uploading

Post by Benjamin »

I ran the code locally and the resized image was perfect until it reached the addslashes line. This means that the code works but your running into a problem somewhere along the line inserting it into your database.
danielfs1
Forum Newbie
Posts: 13
Joined: Thu Feb 14, 2008 10:37 am
Location: Georgia

Re: $_FILES problem with uploading

Post by danielfs1 »

Alright, I managed to find some more information and some other examples.

Here is some code that I got to work with some tweaking.

Code: Select all

 
//IMAGE UPLOAD AND RESIZING
 
    $tmpName  = $_FILES['binFile']['tmp_name'];
    $name = $_FILES['binFile']['name'];
    $size = $_FILES['binFile']['size'];
    $fileType = "image/jpeg";   
    $quality = 100;
    $fp = fopen($tmpName, 'rb');
    $content = fread($fp, filesize($tmpName));
    $im = imagecreatefromstring($content);
    $width = imagesx($im);
    $height = imagesy($im); 
    $imgh = 200;    
    $imgw = $width / $height * $imgh;
    $thumb = imagecreatetruecolor($imgw, $imgh);    
    imagecopyresampled($thumb,$im,0,0,0,0,$imgw,$imgh,$width,$height);  
    ob_start(); 
    imagejpeg($thumb, "" ,$quality);    
    $mynewimage = ob_get_contents();               
    ob_end_clean();
    $mynewimage = addslashes($mynewimage);
    $sql = $conn->query("INSERT INTO user_extra (user_ID) VALUES ('$id') ON DUPLICATE KEY UPDATE  content = '$mynewimage', size = '$size', name = '$name', type = '$fileType'");
 
Post Reply