I am new the the php world because a customer of mine wanted a upload script in php so here I am...I have a script that I am using and it is great except that it does not check if a file already exist and returns a message to that effect. It just uploads and overwrites the existing file. I am under pressure to have this up and running and I need all the help I can get. Here is the script:
<?
$UPLOAD_PATH = ""; // directory on the server where are you going to upload files
// on Unix servers directories look like "/home/users/jack/files/"
// Don't forget about final slashes!!!
$UPLOAD_NUM = 5; // the number of upload fields
// display uploaded file list or errors during uploading
// and upload files
function displayUploadedList ()
{
global $UPLOAD_PATH,
$UPLOAD_NUM,
$HTTP_POST_FILES;
$buff = '';
$error = 0;
for ($i = 0; $i < $UPLOAD_NUM; $i++)
{
$name = $HTTP_POST_FILES["File$i"]['name']; // this is the real name of your file
$tmp = $HTTP_POST_FILES["File$i"]['tmp_name']; // this is the temporary name of your file in temporary
// directory on the server
if (!is_uploaded_file ($tmp)) // is this temporary file really uploaded?
continue;
$buff .= "<li><span class=Text><b>".$name."</b></span>";
if (!@move_uploaded_file($tmp, $UPLOAD_PATH."/".$name)) // move temporary file to your upload directory
$error = 1;
}
if (strlen ($buff) == 0)
{
?>
<span class=Warning>Select at least one file to upload.</span>
<?
return false;
}
else if ($error)
{
?>
<span class=Error>Some of selected files have not been uploaded.</span><br>
<span class=Text>Check existence of upload directory: <b><?=$UPLOAD_PATH?></b> and your permissions.</span>
<?
return false;
}
?>
<span class=Message>The following files have been uploaded</font></span>
<span class=Text><ul><?=$buff?></ul></span>
<?
return true;
}
// display upload form
// this is not interesting function ;-(
function displayUploadForm () // display upload form,
{
global $UPLOAD_NUM;
$tablePre = "
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<td bgcolor=#C0C0C0 align=left valign=top>
<table border=0 cellspacing=1 cellpadding=3>
<tr>
<td class=TableHeader align=left valign=middle colspan=8 bgcolor=#C0C0C0>Please select files to upload</td>
</tr>
<tr bgcolor=#FFFFFF>
<td class=TableHeader align=left valign=middle colspan=8 height=1></td>
</tr>
";
$displayed = '';
for ($i = 0; $i < $UPLOAD_NUM; $i++)
{
$displayed .= "<td class=TableElement bgcolor=#FFFFFF><input type=file name=File$i length=25></td></tr>";
}
$tableSuf = "
</table>
</td>
</tr>
</table>
<p><input type=submit name=Upload value=\"Upload\" style=\"font-size: 10pt;\"></p>";
print $tablePre.$displayed.$tableSuf;
}
?>
<html>
<head>
<title></title>
<link rel=stylesheet type=text/css href=./styles.css>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height=100% border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" bgcolor="#C0C0C0">
<table width="100%" height=100% border="0" cellspacing="1" cellpadding="5">
<tr>
<td bgcolor="#C0C0C0" width="25" height=20> </td>
<td bgcolor="#FFFFFF" colspan=2>
<?
print "<font class=Text><b><center></b></font></center>";
?>
</td>
</tr>
<tr> <form method=POST enctype="multipart/form-data" action="./index.php" >
<td align="left" valign="top" bgcolor="#FFFFFF" width="25"> </td>
<td align="left" valign="top" bgcolor="#FFFFFF" height=100%>
<?
displayUploadForm ();
?>
Please be patient as the upload process could take some time to complete depending upon how large the file(s) are and your internet connection. Once complete, the files you have uploaded will be shown on the right side of this page.<br><BR>
</td>
<td align="left" valign="top" bgcolor="#FFFFFF" width="50%">
<?
if (isset ($Upload)) // if user has clicked on Upload button we must try to upload something
displayUploadedList ();
?>
</td>
</form>
</tr>
<tr>
<td bgcolor="#C0C0C0" colspan="2" height=20><span class=Text><b></b></span></td>
</tr>
</table>
</tr>
</table>
</body>
</html>