Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-08-2005, 07:35 PM multiple images
Junior Talker

Posts: 1
Trades: 0
hi,

i am using some code to insert an image into a mySQL DB,

The code below inserts an image into mySQL database

I have amended it a bit the red code below,
I am tryin to inserts 2 images on the one submit,

I believe that the image is loaded into a temp folder on the server first then written to the database, but not sure how to do it for the second image,

help!!

thanks
anto


<?php
include '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">Property:</font></td>
<td><input type="text" name="property" size=50></td>
</tr>
<tr>
<td><font color="red">Price:</font></td>
<td><input type="text" name="price" size=50></td>
</tr>
<tr>
<td><font color="red">Address:</font></td>
<td><input type="text" name="address" size=50></td>
</tr>
<tr>
<td><font color="red">Rooms:</font></td>
<td><input type="text" name="rooms" size=50></td>
</tr>
<tr>
<td><font color="red">Type:</font></td>
<td><input type="text" name="forsale" size=50></td>
</tr>

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

<tr>
<td><font color="red">File 2:</font></td>
<td><input name="thumb" 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="view_users.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($hostName,
$username,
$password)))
showerror();
if (!mysql_select_db("garages4rentdb", $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}\", \"{$property}\", NOW() ,\"{$price}\", \"{$address}\", \"{$rooms}\", \"{$forsale}\", \"{$thumb}\")";
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()
?>
anto_123 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-09-2005, 07:57 AM
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
Well, here's how I'd do it. Please note that I used the $_FILES[][] notation instead of $file_. I also added 2 columns to your query, since the thumbe could be of another filetype than the original image.

PHP Code:
<?php
//Start changed code (line containing if (is_uploaded_file($userfile)))

foreach($_FILES as $name => $file){
  if(
is_uploaded_file($file)){
    switch (
$image[$name]['type']=$file['type'])
    {
      case 
"image/gif";
        
$image[$name]['mimeName'] = "GIF Image";
      break;
      
      case 
"image/jpeg";
        
$image[$name]['mimeName'] = "JPEG Image";
      break;
      
      case 
"image/png";
        
$image[$name]['mimeName'] = "PNG Image";
      break;
      
      case 
"image/x-MS-bmp";
        
$image[$name]['mimeName'] = "Windows Bitmap";
      break;
      
      default:
        
$image[$name]['mimeName'] = "Unknown image type";
    }
    
//Open the uploaded file
    
$fp fopen($file['tmp_name'], "r");
    
    
//Read in the uploaded file
    
$image[$name]['contents'] = fread($fpfilesize($file['tmp_name']));
    
//Escape special characters in the file
    
$image[$name]['contents'] = AddSlashes($image['name']['contents']);
  }
  else
    
$file[$name]['contents'] = NULL;
}
$insertQuery "INSERT INTO files VALUES (NULL, \"{$short}\", \"".$image['userfile']['type']."\", \"".$image['userfile']['mimeName']."\", \"".$image['userfile']['contents']."\", \"{$property}\", NOW() ,\"{$price}\", \"{$address}\", \"{$rooms}\", \"{$forsale}\", \"".$image['thumb']['contents']."\", \"".$image['thumb']['type']."\", \"".$image['thumb']['mimeName']."\")";

//End code piece (line containing if ((@ mysql_query ($insertQuery, $connection)))

?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Reply     « Reply to multiple images
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31817 seconds with 12 queries