Php file upload system note

Source: Internet
Author: User
Tags php file upload
It's a note. In fact, the entire program has been written, although a little ugly, but it's okay .. after all, it is the first program written by myself... the whole system only uses PHP with codes, no JS, no regular expressions... pure PHP... in fact, I will not...
Below is the html code of the original form...
Upload file Form
The code is as follows:


In the form, enctype = "multipart/form-data" indicates the MIME encoding of the form. By default, the encoding format is application/x-www-form-urlencoded and cannot be used for file upload. only multipart/form-data can be used to completely transmit file data, perform the following operations. enctype = "multipart/form-data" is used to upload binary data. the values in form are passed in a binary stream.
This sentence seems useless... I thought this could limit the size of the uploaded file. The size of the uploaded file is only in php. in ini or in the script of the file to be uploaded later, you have set up... in my program, php is directly set. in ini, upload_max_filesize = 6 m.
Below is the upload program
Code
The code is as follows:
$ Uploaddir = "./map /";
$ Typearr = array ("rar", "zip", "w3m", "w3x ");
$ Findstr = array ("/", "\", "", "<", ">"); // filter the name
$ Mapname = str_replace ($ findstr, '', ($ _ POST ['mapname']);
// Get the extension
Function upfiletype ($ filename ){
$ Arr = explode ('.', $ filename );
$ A = count ($ arr)-1;
Return $ arr [$ a];
}
// Whether the string is a pure number
Function isNo ($ str ){
$ Intarr = array ('1', '2', '3', '4', '5', '6', '7', '8 ', '9', '0 ','. ');
$ A = str_replace ($ intarr, '', $ str );
Return ($ a = "")? 1:0;
}
///// Control the window size ///////
Function html (){
Echo <
File Upload

Script
Function ResetSize (){
Self. resizeTo (300,300)
Self. moveTo (300,200)
}
Script


Eof;
}
If (isNo ($ mapname )){
Html ();
Exit ('Enter the correct map name ');
}
// Determine whether a file is uploaded
If (! Is_uploaded_file ($ _ FILES ['map'] ['tmp _ name']) {
Html ();
Exit ("You have not uploaded a file or uploaded a file larger than 6 MB ");
}
// Determine whether the upload is repeated
$ Clintfilename = str_replace ($ findstr, '', $ _ FILES ['map'] ['name']);
If (isset ($ _ COOKIE ['upload']) {
Foreach ($ _ COOKIE ['upload'] as $ value ){
If ($ value = $ clintfilename ){
Html ();
Exit ("Please do not upload again ");
}
}
}
$ Filetype = strtolower (upfiletype ($ _ FILES ['map'] ['name']);
If (! In_array ($ filetype, $ typearr )){
Html ();
Exit ("please upload the correct map file, supporting rar, zip, w3m, w3x ");
}
// Determine whether the server file exists
$ Filename = $ mapname. '.'. $ filetype;
For ($ a = 1; $ fopen = @ fopen ($ uploaddir. $ filename), "r"); $ a ++ ){
$ Filename = $ mapname. "$ a". '.'. $ filetype;
Fclose ($ fopen );
}
// Start file transfer;
$ Filepath = $ uploaddir. $ filename;
If (move_uploaded_file ($ _ FILES ['map'] ['tmp _ name'], $ filepath )){
// Set a cookie to see if duplicate uploads exist.
Setcookie ("upload []", $ clintfilename, time () + 60*12 );
Html ();
Echo "uploaded successfully ";
$ Log = fopen ("upload. log", "a7 ");
$ Logtxt = date ("Ymd "). "| ". date ("H: I "). "| ". $ _ SERVER ["REMOTE_ADDR"]. "| ". $ filename. "\ r \ n ";
Fwrite ($ log, $ logtxt );
}
?>

Because it was the first time to write... there were problems in many places, and it was changed when I was writing...

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.