I am working on a website that I inherited at work and am still learning php so please bear with me.
The following code is used to upload photos for an image gallery, but I am wondering if there is a way to change this to allow me to upload multiple files at once, rather than one at a time. (There are two tables in the MySQL database … one called galleryAlbum and one called gallery)
[php]
$uploaddir = $modulesDir.$moduleDir.“galleryimages/”; // Directory to store the main images
$uploadTHdir = $modulesDir.$moduleDir.“gallerythumbs/”; // Directory to store the thumbnails
$curpage = $_GET[‘curpage’];
echo “<span class=“adminHeader”>Image Gallery Administration
”;
IF ($do == “addImage”) {
echo “<span class=“actionHead”>Add an Image
”;
$page=$_GET[‘page’];
$descriptionPost = addslashes($_POST[‘description’]);
$albumPost = addslashes($_POST[‘album’]);
$image = $_FILES[‘theImage’][‘name’];
if($page==“submit”){$error = chkGalleryElements($image,$albumPost,$descriptionPost);}
IF (($page=="submit")&&($error=="5")){
$maxfilesize = 5000000;
$query = "SELECT MAX(iid) FROM gallery";
$result=mysql_query($query, $connection) or die("Query failed : " . mysql_error());
$maxImageId = max(mysql_fetch_array($result));
IF ($maxImageId==null){
$maxImageId=0;
}
$imageName = $maxImageId + 1;
$uploadfile = "$uploaddir$imageName.jpg";
$uploadTHfile = $uploadTHdir."th_".$imageName.".jpg";
$uploadsize = $_FILES['theImage']['size'];
/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$pext = getFileExtension($image);
$pext = strtolower($pext);
if (($pext != "jpg") && ($pext != "jpeg")) {
print "<h1>ERROR</h1> The file you uploaded had the following extension: $pext.<br>";
print "<p>Please upload an image with the extension .jpg or .jpeg ONLY.<br><br>";
echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
} elseif ($maxfilesize <= $uploadsize){
echo "The image that you have uploaded is too big. The system will accomodate any images up to 5MB and resize them to ensure optimal browsing. If your file is larger than this, please resize the image.";
echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
} else {
if (move_uploaded_file($_FILES['theImage']['tmp_name'], $uploadfile)) {
// The file
$filename = $uploadfile;
$desiredwidth = 390;
$desiredthumb_width = 150;
// Get new dimensions
list($width, $height) = getimagesize($filename);
IF ($width > 390){
$new_width = $desiredwidth;
$new_height = $desiredwidth/$width * $height;
}
ELSE{
$new_width = $width;
$new_height = $height;
}
IF ($width > 150){
$new_thumb_width = $desiredthumb_width;
$new_thumb_height = $desiredthumb_width/$width * $height;
}
ELSE{
$new_thumb_width = $width;
$new_thumb_height = $height;
}
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$image_t = imagecreatetruecolor($new_thumb_width, $new_thumb_height);
$imagethumb = imagecreatefromjpeg($filename);
imagecopyresampled($image_t, $imagethumb, 0, 0, 0, 0, $new_thumb_width, $new_thumb_height, $width, $height);
// Output
imagejpeg($image_p, "$uploadfile", 100);
imagejpeg($image_t, "$uploadTHfile", 100);
$query = "INSERT INTO gallery VALUES ('','$imageName','$descriptionPost','$albumPost')";
mysql_query($query, $connection) or die("Query failed : " . mysql_error());
echo "Thank-you image has been added to the gallery.<br /><br />";
echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
}
else {
echo "File upload is not valid. Check permissions or contact the system administrator.";
echo "<br /><br />";
echo "<a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
}
}
}
ELSE{
//code to display the editor which replaces the textarea.
echo "<script type=\"text/javascript\" src=\""
."$docroot"
."editor/fckeditor.js\"></script>"
."<script type=\"text/javascript\">"
."window.onload = function()"
."{"
."var oFCKeditor = new FCKeditor( 'description','','250','Default','' ) ;"
."oFCKeditor.BasePath = '"
."$docroot"
."editor/' ;"
."oFCKeditor.ReplaceTextarea() ;"
."}"
."</script>";
//editor code end
IF ($error==2){
echo "<font color=\"red\">Please check to ensure that all fields have been filled.</font><br /><br />";
$error==0;
}
echo "<table width=\"100%\">"
."<form enctype=\"multipart/form-data\" action=\"default.php?$urlString&page=submit\" method=\"POST\">"
."<tr><td width=\"120\">"
//<!-- Name of input element determines name in $_FILES array -->
."<b>Choose Image: </b></td><td><input name=\"theImage\" type=\"file\" /></td></tr>"
."<tr><td width=\"80\"><strong>Add to Album:</strong></td><td>"
."<select name=\"album\" size=\"1\">"
."<option selected";
if (($albumPost!=null)||($albumPost!="")){
echo "value=\"$albumPost\">";
$albumNameQuery = "SELECT * from galleryalbum where aid=$albumPost";
$albumNameResult = mysql_query($albumNameQuery, $connection) or die("Query failed : " . mysql_error());
$albumName = mysql_result($albumNameResult,0,"aname");
echo stripslashes($albumName);
}
else{
echo ">";
}
echo "</option>";
$query = "SELECT * FROM galleryalbum";
$result = mysql_query($query, $connection) or die("Query failed : " . mysql_error());
$numrows = mysql_num_rows($result);
for ($i = 0; $i < $numrows; $i++) {
$aname = mysql_result($result,$i,"aname");
$aid = mysql_result($result,$i,"aid");
echo "<option value=\"$aid\">";
echo $aname;
echo "</option>";
}
echo "</select>";
echo "</td></tr>"
."<tr><td colspan=\"2\" height=\"10\"></td></tr>"
."<tr><td colspan=\"2\"><b>Description:</b><br /></td></tr>"
."<tr><td colspan=\"2\" height=\"100\"><textarea name=\"description\" rows=\"8\" cols=\"75\">";
if (($descriptionPost!=null)||($descriptionPost!="")){
echo stripslashes($descriptionPost);
}
echo "</textarea></td></tr>"
."<tr><td colspan=\"2\" height=\"10\"></td></tr>"
."<tr><td colspan=\"2\" height=\"10\">"
."<input type=\"submit\" value=\"Submit\" />"
."</td></tr>"
."</form>"
."</table>";
echo "<br /><br /><a href=\"default.php?$subUrl\"><img src=\"".$modulesDir.$moduleDir."imgs/return.gif\" height=\"31\" width=\"32\" alt=\"Return\" border=\"none\"/></a><span class=\"moduleReturnText\">Return to Gallery Administration</span>";
}
}[/php]