Jump to content

Archived

This topic is now archived and is closed to further replies.

denhamd2

Problem Uploading Images to MySQL database

Recommended Posts

Hi,

I have a problem with my image upload form. When I upload a large image to my MySQL database it tells me it has uploaded ok, but when I go and view it it was only partially uploaded, ie. only the top of the image shows. Smaller images upload fine. Would anyone have any idea what might be going wrong?

Here's my PHP image upload code:

[code=php:0]$binary_junk = addslashes (fread(fopen($img1, "r"), filesize($img1)));   

if ($img1_type == "image/gif" ) {  if ($img1_size < 3000000) { 
$insert_data = "INSERT INTO  tblimage (TechID, binary_junk, filename, filesize, filetype)  VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')"; 
@mysql_query($insert_data)
or  die("The image is either not a valid GIF or is above 2.9MB, thus could not be added.");  }  }

elseif ($img1_type == "image/jpeg") { 
if ($img1_size < 3000000) { 
$insert_data = "INSERT INTO  tblimage (TechID, binary_junk, filename, filesize, filetype)  VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')"; 
@mysql_query($insert_data)
or  die("The image is either not a valid JPEG or is above 2.9MB, thus could not be added.");  }  }

elseif ($img1_type == "image/pjpeg") { 
if ($img1_size < 3000000) { 
$insert_data = "INSERT INTO  tblimage (TechID, binary_junk, filename, filesize, filetype)  VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')"; 
@mysql_query($insert_data)
or  die("The image is either not a valid JPEG or is above 2.9MB, thus could not be added.");  }  }[/code]


Here is my PHP code to view the image:
[code=php:0]$get_image = "select binary_junk,   
filetype from tblimage where img_id = $fileid";   
$get_image_result = @mysql_query($get_image)   
or die("Couldn't get image.");   
$binary_junk = @mysql_result  ($get_image_result,0,"binary_junk");   
$filetype = @mysql_result  ($get_image_result,0,"filetype");   

header("Content-type: $filetype");   

echo "$binary_junk";[/code]




Many thanks in advance

Share this post


Link to post
Share on other sites
do you have the MAX UPLOAD directives set in the upload form?
do you have max upload set in PHP.ini
Is the table and cells where you want to put this stuff have any limitations?
~~best thing to do is set the  cell feild varchar(3000000)~~
check if your host allows that big file uploads.

Share this post


Link to post
Share on other sites
thanks. I dont have any MAX UPLOAD directives set in the upload form, should I?
The table field is a BLOB type so there shouldnt be an issue with this, right?

Share this post


Link to post
Share on other sites
i havent got much with MySQl data types...
MAX UPLOAD well if you dont have it then it is ok
and what about the ini is it set so that it can upload 3000000 bytes?

Share this post


Link to post
Share on other sites
here is all the code i used. if someone could have a look and check it is ok id be very grateful:

HTML Form Page:
[CODE]
<?php require_once('../Connections/capital.php'); ?><?php
mysql_select_db($database_capital, $capital);
$query_productid = "SELECT * FROM properties ORDER BY propertyid DESC";
$productid = mysql_query($query_productid, $capital) or die(mysql_error());
$row_productid = mysql_fetch_assoc($productid);
$totalRows_productid = mysql_num_rows($productid);
?>
<HTML>
<HEAD>
<TITLE></TITLE>
<link href="../adminstyle.css" rel="stylesheet" type="text/css">
</HEAD>

<BODY>
<table width="750" align="center">
  <tr>
    <td></td>
  </tr>
  <tr>
    <td><?php include('nav_inc.php'); ?>
      <h1>Add Photo</h1>
        <FORM action="image_add_2.php"
method="post" enctype="multipart/form-data" name="prod_img" id="prod_img">
          <p>  Choose file to use as a photo for this property: <br>
            <INPUT type="file" name="img1" size="30">
            <input name="propertyid" type="hidden" id="propertyid" value="<?php echo $row_productid['propertyid']; ?>">
  </p>
          <p>
            <INPUT type="submit" name="submit"
value="Submit">
          </p>
    </FORM></td></tr>
</table>
</BODY>
</HTML>
<?php
mysql_free_result($productid);
?>

[/CODE]


Image Upload PHP Page:
[CODE]<?
  Header('Cache-Control: no-cache');
  Header('Pragma: no-cache');
  ?>
<?php require_once('../Connections/capital.php'); ?><?php
mysql_select_db($database_capital, $capital);
$query_Recordset1 = "SELECT * FROM tblimage ORDER BY img_id DESC";
$Recordset1 = mysql_query($query_Recordset1, $capital) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_capital, $capital);
$query_get_latest_property = "SELECT * FROM properties ORDER BY propertyid DESC";
$get_latest_property = mysql_query($query_get_latest_property, $capital) or die(mysql_error());
$row_get_latest_property = mysql_fetch_assoc($get_latest_property);
$totalRows_get_latest_property = mysql_num_rows($get_latest_property);
?>
<?



$db = @mysql_connect($hostname_capital,
$username_capital, $password_capital) or
die("Can't connect to server.");
@mysql_select_db($database_capital, $db) or
die("Can't select database.");


$binary_junk = addslashes (fread(fopen($img1, "r"), filesize($img1)));

if ($img1_type == "image/gif" ) {
if ($img1_size < 3000000) {
$insert_data = "INSERT INTO
tblimage (TechID, binary_junk, filename, filesize, filetype)
VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')";
@mysql_query($insert_data) or
die("The image is either not a valid GIF or is above 2.9MB, thus could not be added.");
}
} elseif ($img1_type == "image/jpeg") {
if ($img1_size < 3000000) {
$insert_data = "INSERT INTO
tblimage (TechID, binary_junk, filename, filesize, filetype)
VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')";
@mysql_query($insert_data) or
die("The image is either not a valid JPEG or is above 2.9MB, thus could not be added.");
}
} elseif ($img1_type == "image/pjpeg") {
if ($img1_size < 3000000) {
$insert_data = "INSERT INTO
tblimage (TechID, binary_junk, filename, filesize, filetype)
VALUES ('$propertyid', '$binary_junk', '$img1_name', '$img1_size', '$img1_type')";
@mysql_query($insert_data) or
die("The image is either not a valid JPEG or is above 2.9MB, thus could not be added.");
}
}
?>
<?php $thefileid = $row_Recordset1['img_id'] + 1; ?>
<link href="../adminstyle.css" rel="stylesheet" type="text/css">
<table width="750" align="center">
  <tr>
    <td></td>
  </tr>
  <tr>
    <td><?php include('nav_inc.php'); ?><h1>Property Photo </h1>
      <p><?php echo "<a href=\"image_add_3.php?fileid=$thefileid\" target=\"_blank\">View what
            you uploaded</a>"; ?></p>
    <?php
mysql_free_result($Recordset1);

mysql_free_result($get_latest_property);
?>
    <form name="form1" method="post" action="image_add_4.php">
      <div align="center">
        <input name="id" type="hidden" id="id" value="<?php echo $row_get_latest_property['propertyid']; ?>">
        <input type="submit" name="Submit" value="Add Another Photo For This Property">
      </div>
    </form></td>
  </tr>
</table>
[/CODE]


Any help would be much appreciated.

Share this post


Link to post
Share on other sites

×

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.