Upload Multiple File and text using HTML form and PHP
hey I'm stuck on a project since long. Well I have a Project in which user upload multiple images and add tile, description. The form is processed, the image files are stored in a folder and creates thumbnails and the same entry is added to the MySQL database. Well I have done the image processing and thumbnail generation using the "foreach ($_FILES as $k=>$v)
". But I'm unable to get the same repeat with the text fields (Title and description). Here is the code for both "form.php
" and "process.php
". Please help.
formprocess.php:-
foreach($_FILES as $k => $v){
if($_FILES[$k]['size'] > 100000){
echo "Image size exceeded";
return false;
}
else {
$add = "../Image_Folder/".$_FILES[$k][name]; // the path with the file name where the file will be stored, upload is the directory name.
//echo $add;
if(!move_uploaded_file ($_FILES[$k][tmp_name], $add)){
echo "Failed to upload file Contact Site admin to fix the problem";
exit;
}
else {
$query = "INSERT INTO Image (img_id, user_id, img_src) VALUES (NULL, '$user_id', '{$_FILES[$k][name]}')";
$result = mysql_query($query);
if(!$result) {
die('Unable to upload Poster to database: ' . mysql_error());
}
else{
$query2 = mysql_query("SELECT img_id FROM Image WHERE img_id = last_insert_id()") or die(mysql_error());
while($row=@mysql_fetch_array($query2, MYSQL_ASSOC)){
$cat_id=$row['img_id'];
}
///////// Start the thumbnail generation//////////////
$n_width = 100; // Fix the width of the thumb nail images
$n_height = 100; // Fix the height of the thumb nail imaage
$tsrc = "../Image_thumbs/".$_FILES[$k][name]; // Path where thumb nail image will be stored
//echo $tsrc;
if (!($_FILES[$k][type] == "image/jpeg" OR $_FILES[$k][type] == "image/gif" OR $_FILES[$k][type] == "image/png")) {
echo "Your uploaded file must be of JPG or GIF or png. Other file types are not allowed<BR>";
exit;
}
////////////////////// Starting of GIF thumb nail creation ///////////
if (@$_FILES[$k][type] == "image/gif")
{
$im = ImageCreateFromGIF($add);
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
$newimage = imagecreatetruecolor($n_width, $n_height);
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage, $tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage, $tsrc);
}
chmod("$tsrc", 0777);
}
////////// end of gif file thumb nail creation //////////
//// start of png thumb nail creation ////
if (@$_FILES[$k][type] == "image/png")
{
$im = ImageCreateFromGIF($add);
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
$newimage = imagecreatetruecolor($n_width, $n_height);
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
if (function_exists("imagepng")) {
Header("Content-type: image/png");
ImagePNG($newimage, $tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage, $tsrc);
}
chmod("$tsrc", 0777);
}
//// end of png thumbnail generation /////
////////////// starting of JPG thumb nail creation //////////
if($_FILES[$k][type] == "image/jpeg"){
$im = ImageCreateFromJPEG($add);
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
$newimage = imagecreatetruecolor($n_width, $n_height);
imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
ImageJpeg($newimage, $tsrc);
chmod("$tsrc", 0777);
}
$sql = "INSERT INTO catalogue_thumb(thumbid, cat_id, thumb_src) VALUES(NULL, '$cat_id', '$tsrc')";
$result_sql = mysql_query($sql);
if(!$result_sql){
echo "Sorry could not save thumbnail to database";
return false;
}
else{
echo "All Images and Thumbs Uploaded successfully";
}
}
}
}
}
?>
</blink>
form.php:-
$upload_image_limit = 6;
while($i++ < $upload_image_limit){
$form_img .= '<label>Image '.$i.': </label>
<input type="file" name="uplimg'.$i.'"><br/>
<label for="name">Enter Prduct Name</label>
<input type="text" maxlength="65" name="img_name"/>
<label for="description">Add Description</label>
<input type="text" maxlength="250" name="img_description"/>
';
}
$htmo .= '<form method="post" enctype="multipart/form-data" action="formprocess.p开发者_如何学JAVAhp">
<fieldset>
'.$form_img.'
<input type="submit" value="Upload" style="margin-left: 50px;" />
</fieldset>
</form>
';
echo $html;
?>
//////////////////////////////////////////////
You should change the name attribute of input text. You have done so for the file field.
name="uplimg'.$i.'"
Add $i to the names of the text-fields too.
精彩评论