I didn't pay much attention to file uploading before. Yesterday I did a simple one but found that there were quite a few problems.
The program code at the beginning is as follows:
uploadimg.php
<?php
//Determine whether it is an upload interface or an implementation interface. If it is an upload interface, display the following HTML page
if(!$_POST['UploadAction']):
?>
<html>
<head>
<title></title>
</head>
<body><table><center>
<form enctype="multipart/form-data" name="SubmitForm" action="" method="post">
<input type="hidden" name="UploadAction" value="1">
<tr><td><input name="UploadFile" type="file" size="30"></td></tr>
<tr><td><input type="submit" name="submit" value="Upload"></td>
<td><input type="reset" name="reset" value="Reset"></td></tr>
</form>
</center></table></body>
</html>
<?php
//If it is an implementation page, display the following HTML page and activate the PHP script
else:
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
//www.knowsky.com initializes the status of UploadAction
$UploadAction=0;
$UploadFile=$_POST['UploadFile'];
if(($UploadFile!="none") && ($UploadFile!=""))
{
$UploadPath=$_SERVER['DOCUMENT_ROOT']."\";
if (!is_writeable($UploadPath))
{
echo "$UploadPath directory is not writable!"; exit;
}
else
{
echo "$UploadPath directory is writable!";
}
$FileName=$UploadFile_name;
if($UploadFile_size<1024)
{
$FileSize=(string)$UploadFile_size."byte";
}
elseif($UploadFile_size<(1024*1024))
{
$FileSize=number_format((double)($UploadFile_size/1024),1)."KB";
}
else
{
$FileSize=number_format((double)($UploadFile_size/(1024*1024)),1)."KB";
}
if(!file_exists($UploadPath.$FileName))
{
if(move_uploaded_file($UploadFile,$UploadPath.$FileName))
{
echo "File $UploadFile_name ($FileSize) uploaded successfully!";
}
else
{
echo "File $UploadFile_name ($FileSize) upload failed!";
}
unlink($UploadPath.$FileName);
}
else
{
echo "The file $UploadFile_name already exists!";
}
}
else
{
echo "You did not select any file to upload, or the uploaded file exceeds $MAX_FILE_SIZE!";
}
?>
<br><a href="uploadimg.php">Return</a>
</body>
</html>
<?php
endif
?>
After running it, I found that the upload had no effect at all, and I couldn't even enter the implementation page. After searching and asking, I learned that $_FILES is needed to obtain the uploaded file,
so I made some changes:
$UploadFile=$_FILES['UploadFile']['tmp_name'];
$UploadFile_size=$_FILES['UploadFile']['size'];
$UploadFile_name=$_FILES['UploadFile']['name'];
$FileName=strtolower($UploadFile_name);
I thought it would be done by now, but an error message appeared:
Warning: move_uploaded_file(F:Serverwww): failed to open stream: Permission denied in F:Serverwwweooolfinishuploadimg.php on line 76
Warning: move_uploaded_file(): Unable to move 'E:WINDOWSTEMPphp34.tmp' to 'F:\Server\www' in F:Serverwwweooolfinishuploadimg.php on line 76
File *****(****bytes) upload failed!
And I was debugging on the WIN system, so there shouldn't be a file permission problem. I looked and looked and thought about it, but I still couldn't figure it out. I posted messages on Lao Lao and Joy for help, but I didn't get the correct answer. A simple file upload took me more than a day and I still couldn't get it done, which made me very frustrated. I couldn't find errors similar to mine on the Internet.
In the end, I had no choice but to think about why I could successfully upload attachments when debugging with sablog? So I carefully looked at the original code for uploading the attachment to sablog, and made the following changes based on the code:
function getextension($filename) {
return substr(strrchr($filename, "."), 1);
}
$extension=getextension($FileName);
if(move_uploaded_file($UploadFile,$UploadPath."temp.".$extension))
//unlink($UploadFile);
unlink($UploadPath."temp.".$extension);
The results were received immediately and the upload was successful!
I use unlink($UploadPath."temp.".$extension); here to immediately delete the files uploaded to the space and save space.