Forum Moderators: coopster
It is currently able toupload one image per entry. I now wish to be able to have 2 images per entry.
Im not too sure how this can be done though, can anyone help?
Add.php
<?php include("picUtil.php");?>
<?php include("dbCon.php");?>
if($submit)
{
$method = nl2br($method);
$ingredients = nl2br($ingredients); //use the PHP date function for the time
$time = gmdate("j M Y", time());
if (is_uploaded_file($_FILES["pic"]['tmp_name']))
{
$picName = uploadFile("pic");
if(! $picName)
{
die(" Error uploading the picture");
}
}
echo "Your product was added to the database. \n\n";
// inserting it into the Products table which we made in the mysql statements before
$result=MYSQL_QUERY("INSERT INTO RecipeTest2 (id,name,publisher,category,summary,pic,ingredients,method)".
"VALUES ('NULL','$name','$publisher','$category','$summary','$picName','$ingredients','$method')");
}
?>
[b]<snip>[/b]
<form action="<? echo $php_self?>" method="post" enctype="multipart/form-data">
<input name="pic" type="file" id="pic">
<input type="submit" name="submit" value="submit">
</form>
[b]PicUtil.php[/b]
[code]
<?php
$MaxFileSize = 50000 ; //size in bytes
$UploadDirectory = "upload" ; //write your upload directory
$FileTypes = array('image/gif','image/pjpeg','image/jpeg','image/png'); //your allowed file types
[b]<snip>[/b]
//do the actual file uploading
function uploadFile($theValue)
{
global $UploadDirectory;
if (substr($UploadDirectory, -1)!= '/') $UploadDirectory .= '/';
[b]<snip>[/b]
$fileName = $_FILES["$theValue"]["name"];
$uploadName = $fileName ;
//check if the file exist , rename it if it exist .
if(file_exists($UploadDirectory . $uploadName))
{
$i = 0 ;
while(true)
{
$uploadName = $i . "_" . $fileName ;
if(!file_exists($UploadDirectory . $uploadName))
break;
$i++;
}
}
//move the uploaded file to correct directory
if (!move_uploaded_file($_FILES["$theValue"]['tmp_name'],$UploadDirectory . $uploadName)){
echo "Uploading Error , you may check folder permission";
return false;
}
else
return $uploadName;
}
function deleteFile($fileName)
{
global $UploadDirectory;
if (substr($UploadDirectory, -1)!= '/') $UploadDirectory .= '/';
chmod($UploadDirectory . $fileName, 0777);
if(unlink($UploadDirectory . $fileName))
{
return true;
}else{
return false;
}
}
?>
[edited by: ergophobe at 9:53 pm (utc) on Oct. 4, 2004]
[edit reason] Code dump snipped - see forum charter [/edit]
Firstly, you need to create an array of upload fields. You do this by adding [] to the name of the file field.
<input name="pic[]" type="file" id="pic">
<input name="pic[]" type="file" id="pic">
<input name="pic[]" type="file" id="pic">
Then when you process the form do something like:
for($i=0; $i<count($_FILES['pic']['tmp_name']); $i++)
{
$name = $_FILES['pic']['name'][$i];
$temp = $_FILES['pic']['tmp_name'][$i];
$type = $_FILES['pic']['type'][$i];
$size = $_FILES['pic']['size'][$i];
//rest of code
}
Hopefully that helps. :)