PDA

View Full Version : HELP! upload multiple files.


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.

eshban
07-22-2005, 09:52 AM
plz contact me at eshban@gmail.com and send me your code.

I worked on this and have a solution

djluigi
07-22-2005, 02:10 PM
solution is...?

oracleguy
07-22-2005, 05:23 PM
plz contact me at eshban@gmail.com and send me your code.

I worked on this and have a solution

Yeah, don't keep it to yourself, share, thats what forums are for that way a year down the road if some other developer is having the same problem and they google it they might find this thread and see the solution to their problem.

djluigi
07-22-2005, 10:01 PM
thats how i found this post :D

jon.php
07-22-2005, 10:58 PM
and it helps so the same question doesn't get asked over and over again.

JPM
07-23-2005, 02:25 AM
Wouldnt it be possible to upload an non image file (php etc)?

Integrated-Tech
07-23-2005, 05:06 AM
Possible, yes. But if you were to put a SMALL amout of security into your script, say, by checking the file type when it's trying to be uploaded:

case 'upload';

$filetype = $_FILES['image']['type'];
$mimestring = substr($filetype, 0, 15);

switch($mimestring) {

case 'image/jpeg':
include 'scripts/upload.php';
break;
case 'image/pjpeg':
include 'scripts/upload.php';
break;
case 'image/png':
include 'scripts/upload.php';
break;
case 'image/x-png':
include 'scripts/upload.php';
break;
case 'image/gif':
include 'scripts/upload.php';
break;
case 'image/bmp':
include 'scripts/upload.php';
break;

default:
include 'scripts/invalid_file.php';
break;
}
else {
include 'scripts/family_login.php';
}

Then, php will deny those other file types.

I would also recommend you become familiar with the $_FILES superglobal and it's subsets.

Hope this helps

Integrated-Tech

badg0003
07-24-2005, 02:07 PM
Possible, yes. But if you were to put a SMALL amout of security into your script, say, by checking the file type when it's trying to be uploaded:

case 'upload';

$filetype = $_FILES['image']['type'];
$mimestring = substr($filetype, 0, 15);

switch($mimestring) {

case 'image/jpeg':
include 'scripts/upload.php';
break;
case 'image/pjpeg':
include 'scripts/upload.php';
break;
case 'image/png':
include 'scripts/upload.php';
break;
case 'image/x-png':
include 'scripts/upload.php';
break;
case 'image/gif':
include 'scripts/upload.php';
break;
case 'image/bmp':
include 'scripts/upload.php';
break;

default:
include 'scripts/invalid_file.php';
break;
}
else {
include 'scripts/family_login.php';
}

Then, php will deny those other file types.

I would also recommend you become familiar with the $_FILES superglobal and it's subsets.

Hope this helps

Integrated-Tech


I've read somewhere - I believe in the user comments section on the PHP Manual pages that you can't always trust the content contained in the "type" element of the $_FILES global. I, or actally they, recommend using exif_imagetype (http://ca.php.net/manual/en/function.exif-imagetype.php).