djluigi
07-21-2005, 05:59 PM
Hi everyone..
I am having a bit of trouble uploading multiple images.
I had success on uploading one image to specific dir and other info into sql db, but when I try to upload multiple images, i fail.... I need help !!
Here is my code..
Upload Start Form
<form name="FormName" action="form.php" method="post">
<p>Image Name<input type="text" name="filename" size="24"><br></p>
<p>Image Type<select name="image_type" size="1">
<option value="jpg">Jpeg</option>
<option value="gif">Gif</option>
</select><br>
<input type="submit" name="submitButtonName"></p>
</form>
form.php-----------------------
<?php
$image_type = $_POST['image_type'] ;
$filename = $_POST['filename'] ;
$connection = mysql_connect ("localhost", "username", "password")
or die (mysql_error());
//select the database
$db = mysql_select_db("databasename", $connection)
or die (mysql_error());
$sql = "INSERT INTO images
(image_type, filename) VALUES
('$_POST[image_type]', '$_POST[filename]')" ;
$result = mysql_query($sql, $connection)
or die (mysql_error());
$upload_id = mysql_insert_id();
?>
<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffff">
<p>
<form action="upload2.php" method="post" ENCTYPE="multipart/form-data">
Now choose your image to upload<br>
File: <input type="file" name="file[]" size="30"> <br>
File: <input type="file" name="file[]" size="30"><br>
<input type="hidden" value="<? echo $upload_id ?>" name="image_id"><input type="submit" value="Upload!">
</form>
</body>
</html>
upload2.php--------------------
<?php
$image_id = $_POST['image_id'] ;
$file = $_FILES['file'];
//make new directory for uploaded files
mkdir("/path to upload directory/$image_id", 0755);
$target_path = "../images/sales/$image_id/";
$target_path = $target_path . basename( $_FILES['file']['name']);
for($i = 0; $i < sizeof($file); $i++)
{
if($file['error'] == 0) //if a file was uploaded in this field
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "<img src=\"$target_path\" border=\"0\"> " ;
echo " <br>My image path in the server is <b>$target_path</b>";
echo "<br>My file name is - $file_name" ;
echo "<br>My last image ID was $image_id" ;
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
help......please.
I am having a bit of trouble uploading multiple images.
I had success on uploading one image to specific dir and other info into sql db, but when I try to upload multiple images, i fail.... I need help !!
Here is my code..
Upload Start Form
<form name="FormName" action="form.php" method="post">
<p>Image Name<input type="text" name="filename" size="24"><br></p>
<p>Image Type<select name="image_type" size="1">
<option value="jpg">Jpeg</option>
<option value="gif">Gif</option>
</select><br>
<input type="submit" name="submitButtonName"></p>
</form>
form.php-----------------------
<?php
$image_type = $_POST['image_type'] ;
$filename = $_POST['filename'] ;
$connection = mysql_connect ("localhost", "username", "password")
or die (mysql_error());
//select the database
$db = mysql_select_db("databasename", $connection)
or die (mysql_error());
$sql = "INSERT INTO images
(image_type, filename) VALUES
('$_POST[image_type]', '$_POST[filename]')" ;
$result = mysql_query($sql, $connection)
or die (mysql_error());
$upload_id = mysql_insert_id();
?>
<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffff">
<p>
<form action="upload2.php" method="post" ENCTYPE="multipart/form-data">
Now choose your image to upload<br>
File: <input type="file" name="file[]" size="30"> <br>
File: <input type="file" name="file[]" size="30"><br>
<input type="hidden" value="<? echo $upload_id ?>" name="image_id"><input type="submit" value="Upload!">
</form>
</body>
</html>
upload2.php--------------------
<?php
$image_id = $_POST['image_id'] ;
$file = $_FILES['file'];
//make new directory for uploaded files
mkdir("/path to upload directory/$image_id", 0755);
$target_path = "../images/sales/$image_id/";
$target_path = $target_path . basename( $_FILES['file']['name']);
for($i = 0; $i < sizeof($file); $i++)
{
if($file['error'] == 0) //if a file was uploaded in this field
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "<img src=\"$target_path\" border=\"0\"> " ;
echo " <br>My image path in the server is <b>$target_path</b>";
echo "<br>My file name is - $file_name" ;
echo "<br>My last image ID was $image_id" ;
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
help......please.