images in 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
gecko
Forum Commoner
Posts: 34
Joined: Thu Oct 24, 2002 3:45 am

images in database

Post by gecko »

i have heard different opinions about whether to store images in a database, or whether to put a link to each image into the database, while putting the images in a specific folder on the server. the opinions are mostly not more than a it is better to do this or better to do that, without hearing any arguments. is there anyone who could fill me in on this.

anyway meanwhile i have a script that works fine for uploading one image
to my database. can anyone tell me however what i should change to the script, if i would like to upload two images in one record using one and the same form.

here follows my current script (my provider still uses a php version from before the superglobals becam hip, 4.0.1 i believe with register globals on).

thanks hansie

<?php
include "common_db.inc";

if (empty($short) || empty($userfile))
{
?>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload an Image File</title>
</head>
<body bgcolor="white">
<form method="post" action="insert.php" enctype="multipart/form-data">
<h1>Upload an Image File</h1>
<h3>Please fill in the details below to upload your file.
Fields shown in <font color="red">red</font> are mandatory.</h3>
<table>
<col span="1" align="right">

<tr>
<td><font color="red">Short description:</font></td>
<td><input type="text" name="short" size=50></td>
</tr>

<tr>
<td><font color="red">File:</font></td>
<td><input name="userfile" type="file"></td>
</tr>

<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
</form>
<h3>Click <a href="index.php">here</a> to browse the images instead.</h3>
</body>
</html>
<?php
}
else
{
$short = clean($short, 50);
$userfile = clean($userfile, 50);

if (!($connection = @ mysql_pconnect($dbhost,
$dbusername,
$dbuserpassword)))
showerror();

if (!mysql_select_db("test", $connection))
showerror();

// Was a file uploaded?
if (is_uploaded_file($userfile))
{

switch ($userfile_type)
{
case "image/gif";
$mimeName = "GIF Image";
break;
case "image/jpeg";
$mimeName = "JPEG Image";
break;
case "image/png";
$mimeName = "PNG Image";
break;
case "image/x-MS-bmp";
$mimeName = "Windows Bitmap";
break;
default:
$mimeName = "Unknown image type";
}

// Open the uploaded file
$file = fopen($userfile, "r");

// Read in the uploaded file
$fileContents = fread($file, filesize($userfile));

// Escape special characters in the file
$fileContents = AddSlashes($fileContents);
}
else
$fileContents = NULL;

$insertQuery = "INSERT INTO files VALUES (NULL, \"{$short}\", \"{$userfile_type}\", \"{$mimeName}\", \"{$fileContents}\")";

if ((@ mysql_query ($insertQuery, $connection)) && @ mysql_affected_rows() == 1)
header("Location: receipt.php?status=T&file=" . mysql_insert_id($connection));
else
header("Location: receipt.php?status=F&file=" . mysql_insert_id($connection));
} // if else empty()
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may use something like

Code: Select all

...
&lt;?php for($i=0; $i=$numUploadSections; $i++) {  // closing tag here -&gt; ?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;font color="red"&gt;Short description:&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text&#1111;&lt;?php echo $i; // closing tag here -&gt;?&gt;]" name="short" size=50&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;font color="red"&gt;File:&lt;/font&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="userfile&#1111;&lt;?php echo $i; // closing tag here -&gt;?&gt;]" type="file"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php } // closing tag here -&gt;?&gt;
...
take a look at

Code: Select all

echo '&lt;pre&gt;';
print_r($_POST);
print_r($_FILE);
echo '&lt;/pre&gt;';
to see how this effects the part of your script that handles the upload (with php-version >= 4.1 and register_globals off)
Post Reply