Page 1 of 1

Make image into database

Posted: Fri May 21, 2004 2:37 pm
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

Posted: Fri May 21, 2004 2:43 pm
by feyd
looks like it should load any known (to php) image type just fine..

Posted: Fri May 21, 2004 4:07 pm
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

Posted: Fri May 21, 2004 6:50 pm
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.

Posted: Fri May 21, 2004 11:39 pm
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

Posted: Sat May 22, 2004 12:12 am
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> 

?>

Posted: Sat May 22, 2004 5:35 am
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.

Posted: Sat May 22, 2004 8:15 am
by doggy
After doing that now i get this error
ERROR: Column 'data' cannot be null

Posted: Sat May 22, 2004 8:18 am
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.