Link to home
Create AccountLog in
Avatar of m0tSiE
m0tSiE

asked on

PHP Multiple File Uploader, Check File Exists?

Hi,

I'm currently using the below multiple file uploader, however it doesn't check to see if any of the files exist before it uploads the file and in turn overwrites the original file.

Can anyone edit the script to check if the file to be uploaded already exists? or is there a better script I can try that will do this?

Many thanks,

Paul.


uploader.php
<?php
$max_no_img=4; // Maximum number of images value to be set here
echo "<form method=post action=upload.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
?>

upload.php:
<?php
$imgCount = 0;
$filenamesArr = array();
while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value)){   // this will check if any blank field is entered
$filename = $value;    // filename stores the value
$add = "../svr2008/images/caws/$filename";   // upload directory path is set
//echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
// echo "<br>";                             // Display a line break
copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
chmod("$add",0777);                 // set permission to the file.
           
$imgCount++;
$filenamesArr[$imgCount] = $filename;
}
}
$max_no_img=4; // Maximum number of images value to be set here
for($i=1; $i<=$max_no_img; $i++){
echo "Images $i : ".$filenamesArr[$i]." <br>";
}
?>
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Try this:
uploader.php
<?php
$max_no_img=4; // Maximum number of images value to be set here
echo "<form method=post action=upload.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>"; 
?> 
upload.php:
<?php
$imgCount = 0;
$filenamesArr = array();
while(list($key,$value) = each($_FILES[images][name]))
{
	if(!empty($value)){   // this will check if any blank field is entered
		$filename = $value;    // filename stores the value
		$add = "../svr2008/images/caws/$filename";   // upload directory path is set
		if(!file_exists($add) )
		{
			//echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
			// echo "<br>";                             // Display a line break
			copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
			chmod("$add",0777);                 // set permission to the file.
            
			$imgCount++;
			$filenamesArr[$imgCount] = $filename;
		}
	}
}
$max_no_img=4; // Maximum number of images value to be set here
for($i=1; $i<=$max_no_img; $i++){
	echo "Images $i : ".$filenamesArr[$i]." <br>";
}
?>

Open in new window

NOTE: $imgCount contains the "ACTUAL" files that were copied onto the server. Meaning, it does not include the "skipped" duplicates. IF this is what you want then your for-loop should read:
for($i=1; $i<=$imgCount; $i++){
      echo "Images $i : ".$filenamesArr[$i]." <br>";
}

Otherwise, if you want to give the "impression" that even the duplicate files were copied onto the server, you can use:
upload.php:
<?php
$imgCount = 0;
$filenamesArr = array();
while(list($key,$value) = each($_FILES[images][name]))
{
      if(!empty($value)){   // this will check if any blank field is entered
            $filename = $value;    // filename stores the value
            $add = "../svr2008/images/caws/$filename";   // upload directory path is set
            if(!file_exists($add) )
            {
                  //echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
                  // echo "<br>";                             // Display a line break
                  copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
                  chmod("$add",0777);                 // set permission to the file.
           
            }
            $imgCount++;
            $filenamesArr[$imgCount] = $filename;
      }
}
$max_no_img=4; // Maximum number of images value to be set here
for($i=1; $i<=$max_no_img; $i++){
      echo "Images $i : ".$filenamesArr[$i]." <br>";
}
?>
Avatar of m0tSiE
m0tSiE

ASKER

Thanks hielo, i'm using the one in your first reply which works great, but is it possible to display a message to say there is already a file on the server with this name instead of displaying nothing?

Thanks :)
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of m0tSiE

ASKER

Thanks hielo, worked great! :D