Jump to content

Recommended Posts

For SOME reason, when I upload an image file to my database table using my form, when it uploads to the database it is corrupt.  From what I can tell, the image isn't fully uploaded.  For instance, a GIF that is 16kb will be uploaded and only 12kb of data will be there.  I am using phpMyAdmin and the image is going into a LONG_BLOB.  I've tried changing the BLOB type, and nothing helps.  Every image uploaded is corrupt.  However, when I upload the image IN phpMyAdmin, the file is fine.

 

Here is my upload script:

 

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

$filename = $_FILES['cf_photo']['name'];
$filetype = $_FILES['cf_photo']['type'];
$filesize = $_FILES['cf_photo']['size'];
$tmp_filename = $_FILES['cf_photo']['tmp_name'];

$fp = fopen($tmp_filename, 'r');
$filedata = fread($fp, filesize($tmp_filename));
$filedata = mysql_real_escape_string(stripslashes($filedata));
fclose($fp);

$cf_band = mysql_real_escape_string(stripslashes($_POST['cf_band']));
$cf_title = mysql_real_escape_string(stripslashes($_POST['cf_title']));
$cf_body = mysql_real_escape_string(stripslashes($_POST['cf_body']));
$cf_web = mysql_real_escape_string(stripslashes($_POST['cf_web']));
$cf_author = mysql_real_escape_string(stripslashes($_POST['cf_author']));
$cf_timestamp = mysql_real_escape_string(stripslashes($_POST['cf_timestamp']));

$query="INSERT INTO reviews VALUES ('','$cf_band', '$cf_title', '$filedata', '$filename', '$filetype', '$cf_body', '$cf_web', '$cf_author', '$cf_timestamp')";
mysql_query($query);

echo '<b>Post Successfully Added!</b><br /><br />';

mysql_close();

 

Anyone have any idea what's going on?  These files are TOO SMALL to be having issues due to size.

$filedata = mysql_real_escape_string(stripslashes($filedata));

 

You should only use stripslashes() if escape characters were added by the magic_quotes (either _gpc or _runtime depending on how the data is being processed and note $_FILES data is not affected by magic_quotes_gpc) settings.

 

Unconditionally stripping slashes from an image will remove \ characters that are part of the data.

 

Since you are using file functions to read the file, you should turn magic_quotes_runtime off either in a php.ini file, in a .htaccess file (when available), or in your script.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.