I have an upload script compliments of Maaking.com, for uploading multiple images up to my property images directory and also to add the path of these images to my 'images' database. I've modified the script by adding a mySQL query right after the image has been uploaded and changing how the form is presented. I pass the URL parameters for the 'propertyid' and the amount of files that will be uploaded. I've been successful uploading files (and updating the database) when I set the variable $num_files (the number of files to be uploaded) to a number like 12, but when I use the URL parameter, it does not work. It shows the right amount of input boxes, but when I click upload, nothing happens. I used settype to make the URL parameter an integer, so I am stumped.
<?php require_once('../Connections/connProperties.php'); ?>
<?php
/*
By using this script you will be able to upload as many files as you want.
The code will check if file existes, limited extensions, file size, file selected ..etc.
For Q. E-mail/MSN: m(at)maaking.com.
*/
###########################################
#----------Upload Multiple Files----------#
#----------Multi-files Uploader-----------#
#-------------Multi-Uploader -------------#
###########################################
/*=========================================\
Author : Mohammed Ahmed(M@@king) \\
Version : 1.0 \\
Date Created: Aug 20 2005 \\
---------------------------- \\
Last Update: Aug 31 2005 \\
---------------------------- \\
Country : Palestine \\
City : Gaza \\
E-mail : m@maaking.com \\
MSN : m@maaking.com \\
AOL-IM : maa2pal \\
WWW : http://www.maaking.com \\
Mobile/SMS : 00972-599-622235 \\
\\
===========================================\
------------------------------------------*/
$colname_files = "-1";
if (isset($_GET['image_upload'])) {
$colname_files = $_GET['image_upload'];
}
$colname_files2 = "-1";
if (isset($_GET['propertyid'])) {
$colname_files2 = $_GET['propertyid'];
}
//upload directory.
//change to fit your need eg. files, upload .... etc.
$upload_dir = "images/";
//number of files to upload.
settype ($colname_files, int);
$num_files = $colname_files;
//the file size in bytes.
$size_bytes =100200; //51200 bytes = 50KB.
//Extensions you want files uploaded limited to.
$limitedext = array(".gif",".jpg",".jpeg",".png");
//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
}
//if the form has been submitted, then do the upload process
//infact, if you clicked on (Upload Now!) button.
if (isset($_POST['upload_form'])){
echo "<h3>Upload results:</h3>";
//do a loop for uploading files based on ($num_files) number of files.
for ($i = 1; $i <= $num_files; $i++) {
//define variables to hold the values.
$new_file = $_FILES['file'.$i];
$file_name = $new_file['name'];
//to remove spaces from file name we have to replace it with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_tmp = $new_file['tmp_name'];
$file_size = $new_file['size'];
#-----------------------------------------------------------#
# this code will check if the files was selected or not. #
#-----------------------------------------------------------#
if (!is_uploaded_file($file_tmp)) {
//print error message and file number.
echo "File $i: Not selected.<br>";
}else{
#-----------------------------------------------------------#
# this code will check file extension #
#-----------------------------------------------------------#
$ext = strrchr($file_name,'.');
if (!in_array(strtolower($ext),$limitedext)) {
echo "File $i: ($file_name) Wrong file extension. <br>";
}else{
#-----------------------------------------------------------#
# this code will check file size is correct #
#-----------------------------------------------------------#
if ($file_size > $size_bytes){
echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
}else{
#-----------------------------------------------------------#
# this code check if file is Already EXISTS. #
#-----------------------------------------------------------#
if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already exists.<br>";
}else{
#-----------------------------------------------------------#
# this function will upload the files. :) ;) cool #
#-----------------------------------------------------------#
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
echo "File $i: ($file_name) Uploaded.<br>";
mysql_select_db($database_connProperties, $connProperties);
$path = $upload_dir.$file_name;
$propertyid = $_POST['propertyid'];
$i = '1';
$query = "INSERT INTO propertiesimages (propertyid, path, picorder) ".
"VALUES ('$propertyid', '$path', '$picorder')";
mysql_query($query) or die(mysql_error());
}else{
echo "File $i: Faild to upload.<br>";
}#end of (move_uploaded_file).
}#end of (file_exists).
}#end of (file_size).
}#end of (limitedext).
}#end of (!is_uploaded_file).
}#end of (for loop).
# print back button.
echo "»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form didn't submitted then show it.
}else{
echo " <h3>Main Images (place images in the order you would like them viewed)</h3>
Max file size = ". $size_bytes / 1024 ." KB";
echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
// show the file input field based on($num_files).
$i = 1;
while ($i <= 5) {
echo "Main Image $i: <input type=\"file\" name=\"file". $i ."\"><br>";
$i++;
}
echo "<br><h3>Additional Images</h3>";
$z = 1;
while ($i <= $num_files) {
echo "Image $z: <input type=\"file\" name=\"file". $i ."\"><br>";
$z++;
$i++;
}
echo "<br>";
echo " <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\">
<input name=\"propertyid\" type=\"hidden\" id=\"propertyid\" value=\"$colname_files2\">
<input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
</form>";
}
//print copyright ;-)
echo"<p align=\"right\"><br>Script by: <a href=\"http://www.maaking.com\">maaking.com</a></p>";
?>