i've just tackled this task. here is my code:
the html portion:
<FORM ACTION=\"$PHP_SELF\" NAME=upload_form ENCTYPE=\"multipart/form-data\" METHOD=post">
<h3>Upload a File in <font size=2 color=red>$directory</font>:<INPUT NAME=\"userfile\" TYPE=\"file\">
<input type=\"hidden\" name=\"new_dir\" value=\"$directory\">
<input type=\"hidden\" name=\"admin\" value=\"file_menu\">
<input type=\"hidden\" name=\"file_menu\" value=\"upload_file\">
<input type=\"submit\" value=\"Upload File\" onSubmit=\"passCheck()\"> Overwrite? <input type=\"checkbox\" name=\"overwrite\" value=\"1\"></h3>
<p>
</form>
now once a user clicks on submit, here is the php code:
(this may wrap, my apologies.)
function upload_file($new_dir, $userfile, $userfile_name, $overwrite, $home) {
// incoming:
// $new_dir (the directory path, such as /home/kdavis/www
// $userfile (this is php's file, and wll look like "php31.php"
// $userfile_name (this is the actual filename the user is trying to upload)
// $overwrite (this will be 0 if the user doesn't want to overwrite, 1 if so)
// $home is for redirecting; you may remove it, along with the redirect() function
$message = ""; // this will contain the "overwrite message" if user has selected it.
chdir($new_dir); // this is very important
if ( file_exists($userfile_name) && ($overwrite != '1') ) {
// this means file exists, but user hasn't selected to overwrite it.
echo "\n\n
<body>
<h2>Error: <font color=red><i>$userfile_name</i></font> already exists.</h2>
Please go back and click on the \"overwrite\" checkbox if you wish to really overwrite this file.
</body>
";
exit;
}
else if ( file_exists($userfile_name) && ($overwrite == "1") ) {
// checking to make sure functions.php isn't being overwritten
if ($userfile_name == "functions.php") {
echo "\n\n
<body>
<h2>Error: <font color=red>You CANNOT overwrite <i>functions.php</i> because it already exists.</font></h2>
<body>
";
redirect('3','admin=edit_content', $home);
exit;
}
$message = "<body><p><font size=+1>$userfile_name</font> has been <b>successfully</b> overwritten.</body>";
} // end ifblock where overwriting occurs
else if ( $userfile == "none" ) {
// this means a user hasn't specified a file, "none" is the default NULL for filenames
echo "\n\n
<body>
<h2>Error: <font color=red>You MUST specify a file..</font> redirecting.</h2>
<body>
";
redirect('3','admin=edit_content', $home);
exit;
}
// okay, this is our uploading a new file
if ( ($message != "") || (file_exists($userfile)) ) {
// i use file_exists() to make sure the tempfile has been uploaded.
$fp = fopen($userfile_name, "w"); // create USER SPECIFIED file
copy("$userfile", "$userfile_name"); // copy php's temp file to USER SPECIFIED filename.
fclose ($fp);
unlink("$userfile"); //and delete the temp php file
chmod($userfile_name, 1777); // proper user rights
if ( (filesize($userfile_name)) > 1 ) {
echo "File successfully uploaded.. redirecting..";
}
$file_stats = "\n\n
<body>
<hr>
<h2>File Stats:</h2>
<TABLE border=10 cellpadding=30>
<TR>
<TD>FILE:</TD><TD>$userfile_name</TD>
</TR>
<TD>FILE SIZE:</TD><TD>" .filesize($userfile_name). " bytes</TD>
</TR>";
if ( $message != "" ) { // this will display if they have selected "overwrite"
echo "<TR><TD>$message</td></tr>";
}
echo "
</TABLE>
</body>
";
echo "$file_stats";
redirect('3','admin=edit_content', $home);
} // end else ifblock
}
--endphpcode--
hope this could help, if not, feel free to email me any questions.
cheers,
~kyle