having problems with this script. one image ends up going twice. and some of the images i upload dont seem to get there.. well part of the picture does but not all of it. and i have checked the php.ini file to make sure the file size is allowed etc. heres the script..
<?php
/*
uploads multiple files to a
specified folder, gives them a unique name,
and adds their new names to a MySQL database
In the same record field, separated by a space.
TestTable:
+----+----------+--------------+
| id | filename | description |
|====|==========+===============
|| | |
+----+----------+--------------+
It checks the file mime-type, so even
if a cunning user renames their file extension
it'll point their back at them
You can also specify the size limit you
will allow to be uploaded.
You can name this file to anything you please.
----------------------------------------
CONFIGURE THESE SETTINGS
---------------------------------------- */
// The directory you want to upload to:
$uploaddir = "/var/www/html/framegrabs/";
// The maximum file size you will allow to be uploaded (PHP default is 2mb Max):
$MaxSize = "600000";
// Database server hostname:
$dbhostname = "localhost";
// Database username:
$dbusername = "root";
// Database password:
$dbpassword = "widerscreen";
// Database name:
$dbname = "multiup";
// You may want to move this dection into the execSQL section below..
// We think it's better to recieve a MySQL connection error before wasting time browsing for files..
// Connect to the database:
$db = mysql_connect($dbhostname, $dbusername, $dbpassword);
mysql_select_db($dbname, $db) or die("ERROR: Cannot connect to the database, check script configuration");
//--------------------------------------
// -----
function execSQL($strSQL_filenames)
{
if (!$strSQL_filenames == 0)
{
$query = "INSERT INTO TestTable (id, filename,description) VALUES (NULL,'$strSQL_filenames',NULL)";
// echo "$query";
$result = mysql_query($query) or die("ERROR: cannot INSERT data into database, check your permissions");
}
else
{
echo "NOTE: Please select a file to upload";
}
}
// check the number of files selected
$number_of_files = count($_FILES['userfile']);
// PUT EACH FILE THROUGH THIS LOOP SEPERATELY
for($i=0;$i<=$number_of_files;$i++) {
// The format you want your new file to have
$filename_format = uniqid(img);
// if the filesize is greater the 0 bits or bytes or something, continue with upload..
if (!$_FILES['userfile']['size'][$i] == 0)
{
// If the user tries to upload something larger than you specified, stop it, then inform them..
if ($_FILES['userfile']['size'][$i] > $MaxSize)
{
echo "ERROR: the file you tried to upload is too large";
echo "<br><a href='" . $_SERVER['SCRIPT_NAME'] ."'>Let me try again..</a>";
exit;
}
// declare var for temp file
$tempfile = $_FILES['userfile']['tmp_name'][$i];
// declare var file to be uploaded
$uploadfile = $_FILES['userfile']['name'][$i];
// find out filetype so we can append the extension
// after we rename the file.
$extension = $_FILES['userfile']['type'][$i];
// filetypes here
if (strstr($extension,"jpeg"))
{
$extension=".jpg";
}
elseif (strstr($extension,"gif"))
{
$extension=".gif";
}
else
{
// If someone uploaded an 'unspecified' filetype, then tell them
// and give them another chance..
echo "ERROR: That type of file is not allowed. only gif/jpeg allowed.";
echo "<br><a href='" . $_SERVER['SCRIPT_NAME'] ."'>Let me try that again..</a>";
exit;
}
// copy the temp file to the specified dir
if(copy($tempfile, $uploaddir.$uploadfile))
{
echo "Copy Successfull!<br>";
}
else
{
echo "ERROR: something happened whilst trying to copy the temp file to the specified folder";
}
// rename the file and append the correct extension
if(rename($uploaddir.$uploadfile,$uploaddir.$filename_format.$extension))
{
// all went well, tell the user
echo "The File: " . $_FILES['userfile']['name'][$i] . " was uploaded successfully";
echo " and renamed to $filename_format$extension";
}
else
{
// NO! something happened along the lines of rename()..
echo "ERROR: Problem renaming the file.. $uploadfile";
}
echo "<p>";
if($i=="0")
{
$strSQL_filenames = "$filename_format$extension";
}
elseif($i>0)
{
// keep on adding filenames to the $strSQL_filenames string.
$strSQL_filenames = $strSQL_filenames . " $filename_format$extension";
}
}
// Execute SQL after were finished the loop
if($i==$number_of_files)
{
execSQL($strSQL_filenames);
if($strSQL_filenames)
{
echo "The following data was entered into the database: \"" . $strSQL_filenames ."\"";
}
}
}
?>
<html>
<head>
<title>Upload That File!</title>
</head>
<body>
<form name="form1" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<input type="file" name="userfile[]" style="width:300;"><br>
<input type="file" name="userfile[]" style="width:300;"><br>
<input type="file" name="userfile[]" style="width:300;"><br>
<input type="file" name="userfile[]" style="width:300;"><br>
<input type="submit" value="Upload/Submit">
</form>
</body>
</html>