Make image into database

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
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Make image into database

Post by doggy »

Code: Select all

<?php
if ($_FILES['image']['error'] !== 0)
   {
      Header('Location: index.php?e=' . $_FILES['image']['error']);
   }

   $file_tmp_name = $_FILES['image']['tmp_name'];

   //   Get the dimensions of the image
   $size = getimagesize($file_tmp_name);
   $image_width = $size[0];
   $image_height = $size[1];
   $image_mime = $size['mime'];

   if ($image_mime == '')
   {
      Header('Location: index.php?e=5');
   }

   //   Swap the slashes around on a Windows server otherwise MySQL freaks
   $file_tmp_name = str_replace('\'', '/', $file_tmp_name);

   //   Save it to MySQL - normally I'd use a DB abstraction class, but here we need results fast
   dbi();
   $sql = "
      INSERT INTO
         porn
         (
            data,
            width,
            height,
            mime
         ) VALUES (
            LOAD_FILE('$file_tmp_name'),
            '$image_width',
            '$image_height',
            '$image_mime'
         )
   ";
   mysql_query($sql);
   
   if (mysql_error())
   {
      $error = mysql_error();
   }
   else
   {
      $error = false;
      $id = mysql_insert_id();
   }
   
   mysql_close();
?>
this uploaded images to a database but it only allows gif images how can i change it to make it into jpg as well
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

looks like it should load any known (to php) image type just fine..
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

OK but i get the error -

ERROR: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. -

On any file if its 2 megs or 20 k , so i have no idea why is that
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's either a hidden field in the form posting the image or it's pulling the setting out of your php.ini file. Or rather.. there isn't such a hidden field, which it expects.. and so defaults to 0.
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

Code: Select all

<?php
<?php

include("mainfile.php");

   if (isset($_GET['e']))
   {
      switch ($_GET['e'])
      {
         case 1:
            $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
            break;
         case 2:
            $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
            break;
         case 3:
            $error = 'The uploaded file was only partially uploaded.';
            break;
         case 4:
            $error = 'No file was uploaded.';
            break;
         case 5:
            $error = 'The image MIME type could not be determined - are you sure you uploaded a valid image file?';
            break;
         default:
            $error = 'An un-specified error occured';
      }
   }
   else
   {
      $error = false;
   }
?>
<html>
<head>
   <title>How to store an image into a MySQL BLOB field</title>
   <style>
   BODY, TD {
      font-family: Arial, Helvetica;
      font-size: 12px;
      color: black
   }
   </style>
</head>

<body>

<h1>Images <> MySQL via PHP</h1>

<p>Your web server is configured as follows:</p>

<table border=1>
<tr><td>File Uploads Enabled:</td><td><?=ini_get('file_uploads')?></td></tr>
<tr><td>Upload Max Filesize</td><td><?=ini_get('upload_max_filesize')?></td></tr>
<tr><td>Upload Temp Directory</td><td><?=ini_get('upload_tmp_dir')?></td></tr>
<tr><td>POST Max Size</td><td><?=ini_get('post_max_size')?></td></tr>
</table>

<?php
   if ($error)
   {
      echo "<p><font color=red><b>ERROR: $error</b></font></p>";
   }
?>

<p>
<form action="upload_image.php" method=post enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=ini_get('upload_max_filesize')?>">
Image File: <input type=file name="image" accept="image/jpeg,image/gif,image/x-png"><br>
<input type=submit value="Upload">
</form>
</p>

</body>
</html>
?>
Ok there is the hidden input but it is set to 2 megs. and i am trying to upload 30 k
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

Here is all my pages.

upload_image.php

Code: Select all

<?php

include("mainfile.php");
   if ($_FILES['image']['error'] !== 0)
   {
      Header('Location: index.php?e=' . $_FILES['image']['error']);
   }

   $file_tmp_name = $_FILES['image']['tmp_name'];

   //   Get the dimensions of the image
   $size = getimagesize($file_tmp_name);
   $image_width = $size[0];
   $image_height = $size[1];
   $image_mime = $size['mime'];

   if ($image_mime == '')
   {
      Header('Location: index.php?e=5');
   }

   //   Swap the slashes around on a Windows server otherwise MySQL freaks
   $file_tmp_name = str_replace('\'', '/', $file_tmp_name);

   //   Save it to MySQL - normally I'd use a DB abstraction class, but here we need results fast
   dbi();
   $sql = "
      INSERT INTO
         porn
         (
            data,
            width,
            height,
            mime
         ) VALUES (
            LOAD_FILE('$file_tmp_name'),
            '$image_width',
            '$image_height',
            '$image_mime'
         )
   ";
   mysql_query($sql);
   
   if (mysql_error())
   {
      $error = mysql_error();
   }
   else
   {
      $error = false;
      $id = mysql_insert_id();
   }
   
   mysql_close();
?>
<html>
<head>
   <title>IMG</title>
   <style>
   BODY, TD {
      font-family: Arial, Helvetica;
      font-size: 12px;
      color: black
   }
   </style>
</head>

<body>

<?php
   if ($error)
   {
      echo "<p><font color=red><b>ERROR: $error</b></font></p>";
   }
   else
   {
?>

<p>
Image Uploaded Successfully
</p>

<p>
Image Data:
</p>

<table border=1>
<tr><td>Width:</td><td><?=$image_width?></td></tr>
<tr><td>Height:</td><td><?=$image_height?></td></tr>
<tr><td>Mime Type:</td><td><?=$image_mime?></td></tr>
<tr><td>Image:</td><td><img src="show_image.php?i=<?=$id?>" width="<?=$image_width?>" height="<?=$image_height?>" alt="This came from MySQL!"></td></tr>
</table>

<p>
<a href="index.php">Upload another image</a>
</p>

<?
   }
?>

</body>
</html> 
?>
index.php

Code: Select all

<?php
<?php

include("mainfile.php");

   if (isset($_GET['e']))
   {
      switch ($_GET['e'])
      {
         case 1:
            $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
            break;
         case 2:
            $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
            break;
         case 3:
            $error = 'The uploaded file was only partially uploaded.';
            break;
         case 4:
            $error = 'No file was uploaded.';
            break;
         case 5:
            $error = 'The image MIME type could not be determined - are you sure you uploaded a valid image file?';
            break;
         default:
            $error = 'An un-specified error occured';
      }
   }
   else
   {
      $error = false;
   }
?>
<html>
<head>
   <title>IMG</title>
   <style>
   BODY, TD {
      font-family: Arial, Helvetica;
      font-size: 12px;
      color: black
   }
   </style>
</head>

<body>

<?php
   if ($error)
   {
      echo "<p><font color=red><b>ERROR: $error</b></font></p>";
   }
?>
<p>
<form action="upload_image.php" method=post enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?=ini_get('upload_max_filesize')?>">
Image File: <input type=file name="image" accept="image/jpeg,image/gif,image/x-png"><br>
<input type=submit value="Upload">
</form>
</p>

</body>
</html> 

?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You've missed a script - my show_image.php one :)

I don't think it will make any difference, but change this line:

<input type=file name="image" accept="image/jpeg,image/gif,image/x-png">

To remove the whole "accept" section.
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

After doing that now i get this error
ERROR: Column 'data' cannot be null
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

The error you get now cannot be caused by changing a little HTML tag. Look elsewhere, make sure the tag closes properly, etc.
Post Reply