i don't really know what to tell ya.. i don't understand when you say it is being copied to "$nfile" do you mean the actual filename is called $nfile or the contents of $nfile? if it is the latter, then isn't that correct?
anyways, here is my uploading script i use for an image hosting website i just wrote:
function upload_file($new_dir, $userfile, $userfile_name, $overwrite, $home, $current_user_id) {
global $DB_CONF, $date;
// 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
// $current_user_id is from session variables (disregard anything with it)
$message = ""; // this will contain the "overwrite message" if user has selected it.
$userfile_name = strtolower($userfile_name);
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 index.html isn't being overwritten
if ($userfile_name == "index.html") {
echo "\n\n
<body>
<h2>Error: <font color=red>You CANNOT overwrite <i>index.html</i> because it already exists.</font></h2>
<body>
";
redirect('3','admin=edit_content', $home);
exit;
}
// let's add a message variable for later
$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;
} // end userfile==none
// okay, this is our uploading a new file ifblock (if we make it here, everything is in order)
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, 0777); // proper user rights
if ( (filesize($userfile_name)) > '1' ) {
echo "File uploaded.. <font size=red>successfully</font>.";
}
$file_stats = "\n\n
<body>
<hr>
<h2>File Stats:</h2>
<TABLE border=10 cellpadding=10>
<TR>
<TD>FILE:</TD>
<TD>$userfile_name</TD>
</TR>
<TD>FILE SIZE:</TD>
<TD>" .filesize($userfile_name). " bytes</TD>
";
if ( $message != "" ) { // this will display if they have selected "overwrite"
echo "<TR><TD>$message</td></tr>";
}
echo "
</TR>
</TABLE>
";
echo "$file_stats";
// let's create a link from our newly uploaded file
$link = makelink($new_dir, $userfile_name, $current_user_id);
$link = addslashes($link);
// now we'll grab the email for the user
$email = getemail($current_user_id);
$msg = "Hello,\n\nYour cut and paste codes for the file $userfile_name:\n\n".stripslashes($link)."\n\nThis is an automated message; please do not reply. If you feel that you have received this transmission in error, disregard it.";
// and send the email with their cut and paste codes
sendemail($email, $msg);
// let's add this link to the database
fileinsert_database($userfile_name, $current_user_id, $link);
// and now we'll display the codes in the current browser for convenience
echo "<table width=\"100%\"><tr><td NOWRAP>
<br>Your cut and paste codes for this file:<br><br> <b>".htmlspecialchars(stripslashes($link))."</b><br> (they have been mailed to you as well).<br><br><a href=\"$home?admin=edit_content\">return to file menu</a>
</td>
</tr>
</table></body>";
} // end else ifblock
} // end upload
hope it can help,
~kyle