I have a working multiple image upload. It uploads multiple pictures to my images/regular folder fine, but i also want it to resize when I upload the image. Right now, it only resizes the first one. I also want it to have a naming convention number so if my image name was bellTower, and I upload multiple under the same category, I want it be name like so: bellTower01.jpg, bellTower02.jpg, bellTower03.jpg…bellTower25.jpg. I really need help on this, I 've been stuck on this for two weeks now.
Here is my doUpload.php:
<?php
session_start();
set_time_limit(300);
if($_POST["uploadFile"] !="")
{
$counter = 24;
for ($i=1; $i<6; $i++)
{
$fileExt = strrchr($_FILES['userfile'.$i]['name'],".");
if($fileExt != ".jpg" && $fileExt != ".gif")
{
$_SESSION["badFileType"] = "You cannot upload a file of type ".$fileExt;
}else
{
$fileName = $_FILES['userfile'.$i]['name'];
if(!is_uploaded_file($_FILES['userfile'.$i]['tmp_name']))
{
echo "Problem: possible file upload attack!";
exit;
}
$upfile = "images/regular/".$_POST["category"].$counter.".jpg";
//this is for resize
$newFileName = $_POST["category"].$counter.".jpg";
while (file_exists($upfile)) {
$i++;
$new_counter = sprintf("%02d",$i);
$file_name = substr($newFileName, 0, strlen($pic_name)-6);
$file_extension = substr($newFileName, strlen($newFileName)-6, strlen($newFileName));
$frand = $file_name.$new_counter.$file_extension;
$upfile = "images/regular".$_POST["category"].$new_counter.".jpg";
}
if (isset($new_counter)) {
$upfile = "images/regular/".$_POST["category"].$new_counter.".jpg";
$newFileName = $_POST["category"].$new_counter.".jpg";
}
if(!copy($_FILES['userfile'.$i]['tmp_name'], $upfile))
{
echo "Problem: cannot upload file to directory.";
exit;
}
$_SESSION["badFileType"] = "File uploaded successfully";
$counter++;
} //end of file check and upload
} //end of for loop
}else
{
$_SESSION["badFileType"] = "";
}
/// RESIZING IMAGE
$dir = "./images/regular/";
$thdir = "./images/th/";
$img = $newFileName;
resizejpeg($dir, $thdir, $img, 160, 120, "thumb_");
$_SESSION["badFileType"] .= "<br>File Sucessfully Resized!";
header("Location: upload2.php");
///////////////////////////////////////////////////////////
// function resizejpeg
//
// creates a resized image based on the max width
// specified as well as generates a thumbnail from
// a rectangle cut from the middle of the image.
//
// @dir = directory image is stored in
// @newdir = directory new image will be stored in
// @img = the image name
// @max_w = the max width of the resized image
// @max_h = the max height of the resized image
// @prefix = the prefix of the resized image
//
///////////////////////////////////////////////////////////
function resizejpeg($dir, $newdir, $img, $max_w, $max_h, $prefix)
{
// set destination directory
if (!$newdir) $newdir = $dir;
// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($dir.$img);
// make sure image is a jpeg
if ($or_t == 2)
{
// obtain the image's ratio
$ratio = ($or_h / $or_w);
// original image
$or_image = imagecreatefromjpeg($dir.$img);
// resize image?
if ($or_w > $max_w || $or_h > $max_h) {
// resize by height, then width (height dominant)
if ($max_h < $max_w) {
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}
// resize by width, then height (width dominant)
else {
$rs_w = $max_w;
$rs_h = $ratio * $rs_w;
}
// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate resized image
imagejpeg($rs_image, $newdir.$prefix.$img, 100);
return true;
}
// Image type was not jpeg!
else
{
return false;
}
}
?>
Here is my upload.php page:
<?php
session_start();
echo("<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>");
?>
<!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" xml:lang="en" lang="en">
<head>
<title>Lab 08 - Upload2</title>
<meta http-equiv="Content-Type" content="text/html; UTF-8" />
<style type="text/css">
ul{ list-style:none; margin-top:5px;}
ul li{ display:block; float:left; width:100%; height:1%;}
ul li label{ float:left; padding:7px; color:#6666ff}
ul li input, ul li textarea, ul li select{ float:right; margin-right:10px; border:1px solid #ccc; padding:3px;
font-family: Georgia, Times New Roman, Times, serif; width:60%;}
li input:focus, li textarea:focus{ border:1px solid #666; }
fieldset{ padding:10px; border:1px solid #ccc; width:400px; overflow:auto; margin:10px;}
legend{ color:#000099; margin:0 10px 0 0; padding:0 5px; font-size:11pt; font-weight:bold; }
label span{ color:#ff0000; }
fieldset#info {position:absolute; top:100px; left:20px; width:460px; }
fieldset#submit {position:absolute; top:350px; left:20px; width:460px; text-align:center; }
fieldset input#SubmitBtn{ background:#E5E5E5; color:#000099; border:1px solid #ccc; padding:5px; width:150px;}
div#errorMsg {color:#ff0000; font-weight:bold; font-size:12pt; position:absolute; top:410px; left:25px;}
div#newLogin {color:#0000ff; font-size:12pt; position:absolute; top:350px; left:25px;}
</style>
</head>
<body>
<h1 style="font-size:14pt; text-indent:360px;">Lab 08 - Upload2</h1>
<?php include("includes/menu.php"); ?>
<form id="form0" method="post" action="doUpload2.php" enctype="multipart/form-data">
<fieldset id="info">
<input type="hidden" name="src" value="upload2.php" />
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<legend>Upload Multiple Files</legend>
<ul>
<li> <label title="category" for="category">Category: </label>
<input name="category" id="category" type="text" size="20" maxlength="255" />
</li>
<li> <label title="userfile1" for="userfile1">File 1: <span>*</span></label> <input type="file" name="userfile1" id="userfile1" size="25" /></li>
<li> <label title="userfile2" for="userfile2">File 2: <span>*</span></label> <input type="file" name="userfile2" id="userfile2" size="25" /></li>
<li> <label title="userfile3" for="userfile3">File 3: <span>*</span></label> <input type="file" name="userfile3" id="userfile3" size="25" /></li>
<li> <label title="userfile4" for="userfile4">File 4: <span>*</span></label> <input type="file" name="userfile4" id="userfile4" size="25" /></li>
<li> <label title="userfile5" for="userfile5">File 5: <span>*</span></label> <input type="file" name="userfile5" id="userfile5" size="25" /></li>
</ul>
</fieldset>
<fieldset id="submit">
<input type="submit" id="uploadFile" name="uploadFile" value="Upload File" />
</fieldset>
</form>
<div id="errorMsg"><?php if(!empty($_SESSION["badFileType"])){echo($_SESSION["badFileType"]);} ?></div>
<script type="text/javascript">
document.getElementById("category").focus();
</script>
<?php
$_SESSION["badFileType"] = "";
?>
</body>
</html>
I hope someone can help me out.