How to use PHP to implement file upload example

Source: Internet
Author: User
Tags copy file size file upload implement interface reset variable
Upload how to upload files in PHP (example 1)


Upload File Form page: updatefile.htm
<title> Upload File Form </title>
<body>
<form enctype= "Multipart/form-data" action= "updatefile.php3" method= "POST" >
<input type= "hidden" name= "max_file_size" value= "1000" >
<div align= "Center" ><center> Please select files:
<input name= "UserFile" type= "File" >
<input type= "Submit" value= "Send File" >
</center></div>
</form>
</body>

----------------------------------------------------------------

Processing uploaded Files Web page: updatefile.php3

<meta http-equiv= "Content-type" content= "text/html;
charset=gb2312 ">
<title> processing uploaded files </title>
<body>
?
Copy ($userfile, "NewFileName");
echo $userfile. "-The name of the file that uploaded to the server is temporarily stored <br>";
echo $userfile _name. "-The original name <br>" of the file on the user's machine;
echo $userfile _size. "-The actual number of bytes uploaded file <br>";
echo $userfile _type. "-If the user's browser provides this information,
That represents the MIME type. For example
Image/gif<br> ";
?>
</body>

----------------------------------------------------------------

Note: Files must be copied to a new location or renamed after they are uploaded.
Otherwise, the upload sample will be deleted when the process finishes executing




How to implement a file upload with PHP3 (instance 2)

PHP3 is a very powerful CGI scripting language, with its linguistic features essentially derived from C,
More like Perl in terms of its implementation, and its intrinsic support for the database makes it an ASP
's formidable opponents.

First, there must be an interface at the front desk

<HTML>
<HEAD>
<TITLE> File Upload Interface </TITLE>
</HEAD>
<BODY><TABLE><CENTER>
<form enctype = "Multipart/form-data" NAME = "SubmitForm"
ACTION = "Upload.php3" method = "POST" >
<input TYPE = "hidden" NAME = "max_file_size" VALUE = "1000000" >
<TR>
<td><input NAME = "UploadFile" TYPE = "file" SIZE = "></TD>"
</TR>
<TR>
<td><input NAME = "submit" VALUE = "submit" TYPE = "Submit" ></TD>
<td><input NAME = "reset" VALUE = "reset" TYPE = "Reset" ></TD>
</TR>
</FORM></CENTER></TABLE></BODY>
</HTML>
It is noteworthy that a max_file_size hidden range is set by its value to
To limit the size of the uploaded file.
The file upload operation is then implemented in the background. If just the most basic upload, a few lines
We can handle it.

?
If ($UploadFile!= "None")
{
Copy ($UploadFile, "$UploadFile _name");
Unlink ($UploadFile);
}
Else
{
echo "You didn't choose any file upload!" ";
}
?>
Because the value that the form passes over is automatically assigned to the variable with the same name, it is passed directly through the
$UploadFile can access the uploaded file, but because this is a variable that saves the file,
The file name must go through another $uploadfile_.
The name variable is obtained. Delete the temporary file (unlink) after copying the document.
The above code can work, but the practical application of the loopholes, let us gradually improve the

First, the uploaded file must have a fixed directory to save, and we're here with a
$UploadPath variables are preserved, as

$UploadPath = "/home/flier/upload/";
Or a complex bit of automatic positioning, such as
$UploadPath = Addslashes (dirname
($PATH _translated)). " \\upload\\ ";
$PATH _translated is the name of the current routing directory,
We assume that one of its names is upload
subdirectory to save the uploaded file. The DirName function returns its directory name.
Then add the subdirectory name
Then use a variable $filename to save the full uploaded file name and path
$FileName = $UploadPath. $UploadFile _name;
Secondly, we also want to let users know the upload file's brief information, such as the size of the uploaded file
if ($UploadFile _size <1024)
Upload file size
{
$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). "MB";
}
The Number_format function plays the role of formatted output, please refer to the manual for specific usage
Next we must take into account the existence of the file and the failure of the copy operation, and mention
For the appropriate hint information

if (!file_exists ($FileName))
{
if (copy ($UploadFile, $FileName))
{
echo "File $UploadFile _name
($FileSize) Upload success! ";
}
Else
{
echo "File $UploadFile _name upload failed! ";
}
Unlink ($UploadFile);
}
Else
{
echo "File $UploadFile _name already exists! ";
}
Then we should take into account the large file upload is prone to timeouts, you can use
Set_time_limit ($TimeLimit), increase timeout limit time.
Finally, the section and implementation code are integrated into a single file, in order to achieve this
An idea, we add an implied value to the form
<input TYPE = "hidden" NAME = "uploadaction" VALUE = "1" > indicates
The current state (interface or implementation) so that the complete code is treated as follows
(filename upload.php3)

?
if (! $UploadAction):
?>
<HTML>
<HEAD>
<TITLE> File Upload Interface </TITLE>
</HEAD>
<BODY><TABLE><CENTER>
<form enctype = "Multipart/form-data" NAME = "SubmitForm"
ACTION = "Upload.php3" method = "POST" >
<input TYPE = "hidden" NAME = "max_file_size" VALUE = "1000000" >
<input TYPE = "hidden" NAME = "uploadaction" VALUE = "1" >
<TR>
<td><input NAME = "UploadFile" TYPE = "file" SIZE = "></TD>"
</TR>
<TR>
<td><input NAME = "submit" VALUE = "submit" TYPE = "Submit" ></TD>
<td><input NAME = "reset" VALUE = "reset" TYPE = "Reset" ></TD>
</TR>
</FORM></CENTER></TABLE></BODY>
</HTML>
?
Else
?>
<HTML>
<HEAD>
<TITLE> File Upload Code </TITLE>
</HEAD>
<BODY>
?
$UploadAction = 0;

$TimeLimit = 60; /* Set timeout limit time
The default time is 30 seconds
Set to 0 when not limited to * *
Set_time_limit ($TimeLimit);

If (($UploadFile!= "None") &&
($UploadFile!= ""))
{
$UploadPath = Addslashes (dirname ($PATH _translated)). " \\upload\\ ";
Upload File Store path
$FileName = $UploadPath. $UploadFile _name; Upload file name
if ($UploadFile _size <1024)//upload file size
{
$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. " MB ";
}

if (!file_exists ($FileName))
{
if (copy ($UploadFile, $FileName))
{
echo "File $UploadFile _name ($FileSize) upload succeeded! ";
}
Else
{
echo "File $UploadFile _name upload failed! ";
}
Unlink ($UploadFile);
}
Else
{
echo "File $UploadFile _name already exists! ";
}
}
Else
{
echo "You didn't choose any file upload!" ";
}

Set_time_limit (30); Restore default timeout settings
?>
<br><a HREF = "Upload.php3" > Return </A>
</BODY>
</HTML>
?
endif
?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.