3 Replies - 1435 Views - Last Post: 16 February 2011 - 08:30 AM Rate Topic: -----

#1 Xarim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-November 10

multi images upload - HELP!

Posted 16 February 2011 - 02:02 AM

MULTI IMAGE UPLOAD WITH PHP MYSQL
Hi guys, I'm learning PHP Mysql through forums like yours and you have been most helpful, thank you all for your time.
I managed to build a database where i could save one image and some some extra info but I don't know how to upload several images for the same product your help is appreciated. Please find my code attached: the code I got from this site and I did modify but it's not working.....

The upload form:


<?php 
// Start a session for displaying any form errors
session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dream in code tutorial</title>

<style type="text/css">
label
{
float: left;
text-align: right;
margin-right: 10px;
width: 100px;
color: black;
}

#submit
{
float: left;
margin-top: 5px;
position: relative;
left: 110px;
}

#error
{
color: red;
font-weight: bold;
font-size: 16pt;
}
</style>
</head>

<body>

<div>
<?php
if (isset($_SESSION['error']))
{
echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
unset($_SESSION['error']);
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>
<label>First Name</label>
<input type="text" name="fname" /><br />

<label>Last Name</label>
<input type="text" name="lname" /><br />

<label>Upload Image</label>
<input type="file" name="image" /><br />
<input type="file" name="image1" /><br />
<input type="file" name="image2" /><br />
<input type="file" name="image3" /><br />
<input type="file" name="image4" /><br />

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="submit" id="submit" value="Upload" />
</p>
</form>
</div>
</body>
</html>





And the php code:

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

if (in_array($file['type'], $valid_types))
return 1;
return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$image = $_FILES['image'];

// Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $fname == "" || $lname == "" || $image['name'] == ""|| $image1['name'] == "" || $image2['name'] == "" || $image3['name'] == "" || $image4['name'] == "" )
{
$_SESSION['error'] = "All fields are required";
header("Location: index.php");
exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
header("Location: index.php");
exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
//header("Location: index.php");
exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server

//#### $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";

$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '$image['name']', '$image1['name']','$image2['name']','$image3['name']', '$image4['name']')";




$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
//header("Location: images.php");
exit;
}
else
{
// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
header("Location: index.php");
exit;
}
?>








Is This A Good Question/Topic? 0
  • +

Replies To: multi images upload - HELP!

#2 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: multi images upload - HELP!

Posted 16 February 2011 - 07:32 AM

I am at home and getting ready for work so I can give you a quick solution but when I get to work I can give you a more detailed fix.

But fox now, you can change all your upload fields
<input type="file" name="image" /><br />
<input type="file" name="image1" /><br />
<input type="file" name="image2" /><br />
<input type="file" name="image3" /><br />
<input type="file" name="image4" /><br />


To
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />


What this will do is create a Array of "image"'s so there will be image[0] through image[3], with this being done, you can do a loop.

foreach($image as $upload)
{
  if (move_uploaded_file($upload['tmp_name'], $TARGET_PATH))
  {
    // Your query has incorrect columns
    $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '$image[0]['name']')";

   $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
   exit;
 }
 else
 {
  $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
  header("Location: index.php");
  exit;
 } 
}

This post has been edited by VolcomMky: 16 February 2011 - 07:35 AM

Was This Post Helpful? 2
  • +
  • -

#3 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: multi images upload - HELP!

Posted 16 February 2011 - 07:48 AM

What you can do is use serialize to add the images to a array and save it to the database and when you pull it out use unserialize and it will return as a array and you can use another loop to display multiple images.

You would need to move the sql after the loop

$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '$serializedArrayOfImages')";

Was This Post Helpful? 0
  • +
  • -

#4 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 313
  • Joined: 13-May 09

Re: multi images upload - HELP!

Posted 16 February 2011 - 08:30 AM

Just got to work so I could help a little more. :walkman:

So as I was saying, for multiple images but only 1 image column in the database you can insert it to the column as a array with serialize, below is an example.

Let us know how it works or if anything goes wrong :clap:

$ArrayOfImages = array();
foreach($image as $upload)
{
  if (move_uploaded_file($upload['tmp_name'], $TARGET_PATH))
  {
   array_push($ArrayOfImages,$upload['name']);
   exit;
 }
 else
 {
  $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
  header("Location: index.php");
  exit;
 } 
}
 $serializedImages = serialize($ArrayOfImages);
 $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '$serializedImages')";
 $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1