How to upload php files

Source: Internet
Author: User
Tags php file upload upload php
Use the php file Upload idea and implementation, read the specific idea and implementation of using php file upload. for file upload, we need to use the type = file type of the form in HTML and its enctype attribute. This is what we all need. Of course, the FILE function library, string function library, directory function library, and $ _ FILES [] in the PHP function library are mandatory. <LINKhref = "htt

For file upload, we need to use the type = file type of the form in HTML and its enctype attribute. This is what we all need. Of course, we must use the FILE function library, string function library, directory function library, and $ _ FILES [] in the PHP function library.

Each website may have many restrictions on the upload of files. These restrictions include the file type, file size, extension, the existence of the upload directory, and the existence of the uploaded file, directory writability and readability, renaming uploaded files, and copying files from the cache to the desired directory.

Of course, error preprocessing cannot be ignored! If we further discuss it, we can use the event log record for file operations.

We can use a program to implement these functions:
  
The first is our preset variable value, which includes the file size, file extension type, MIMI type, and whether to delete the switch variable.

$ MAX_SIZE = 2000000;
$ FILE_MIMES = array ('image/jpeg ', 'image/jpg', 'image/GIF'
, 'Image/png ', 'application/msword ');

$ FILE_EXTS = array('.zip', '.jpg ', '.png', '.gif ');

$ DELETABLE = true;

Next, set the browser access variables and directory access variables:

$ Site_name = $ _ SERVER ['http _ host'];
$ Url_dir = http: //. $ _ SERVER ['http _ host']. dirname ($ _ SERVER ['php _ SELF ']);
$ Url_this = http: //. $ _ SERVER ['http _ host']. $ _ SERVER ['php _ SELF '];

$ Upload_dir = files /;
$ Upload_url = $ url_dir./files /;
$ Message =;

Create an upload directory and change the permission accordingly:

If (! Is_dir (files )){
If (! Mkdir ($ upload_dir ))
Die (upload_files directory doesn' t exist and creation failed );
If (! Chmod ($ upload_dir, 0755 ))
Die (change permission to 755 failed .);
}

Processing of user requests:


If ($ _ REQUEST [del] & $ DELETABLE ){
$ Resource = fopen(log.txt, );
Fwrite ($ resource, date (Ymd h: I: s). DELETE-$ _ SERVER [REMOTE_ADDR]. $ _ REQUEST [del] \ n );
Fclose ($ resource );

If (strpos ($ _ REQUEST [del],/.)> 0); // possible hacking
Else if (strpos ($ _ REQUEST [del], files/) === false); // possible hacking
Else if (substr ($ _ REQUEST [del], 0, 6) = files /){
Unlink ($ _ REQUEST [del]);
Print <script> window. location. href = '$ url_this? Message = deleted successfully '</script>;
}
}
Else if ($ _ FILES ['userfile']) {
$ Resource = fopen(log.txt, );
Fwrite ($ resource, date (Ymd h: I: s). UPLOAD-$ _ SERVER [REMOTE_ADDR]
. $ _ FILES ['userfile'] ['name'].
. $ _ FILES ['userfile'] ['type']. \ n );
Fclose ($ resource );

$ File_type = $ _ FILES ['userfile'] ['type'];
$ File_name = $ _ FILES ['userfile'] ['name'];
$ File_ext = strtolower (substr ($ file_name, strrpos ($ file_name ,.)));

// Check the file size:

If ($ _ FILES ['userfile'] ['size']> $ MAX_SIZE)
$ Message = The file size is over 2 MB .;
// File Type/Extension Check
Else if (! In_array ($ file_type, $ FILE_MIMES)
&&! In_array ($ file_ext, $ FILE_EXTS ))
$ Message = Sorry, $ file_name ($ file_type) is not allowed to be uploaded .;
Else
$ Message = do_upload ($ upload_dir, $ upload_url );

Print <script> window. location. href = '$ url_this? Message = $ message' </script>;
}
Else if (! $ _ FILES ['userfile']);
Else
$ Message = Invalid File Specified .;

List uploaded files:

$ Handle = opendir ($ upload_dir );
$ Filelist =;
While ($ file = readdir ($ handle )){
If (! Is_dir ($ file )&&! Is_link ($ file )){
$ Filelist. = <a href = '$ upload_dir $ file'>. $ file. </a>;
If ($ DELETABLE)
$ Filelist. = <a href = '? Del = $ upload_dir $ file 'title = "delete"> x </a>;
$ Filelist. = <sub> <small> <font color = gray>. date (d-m H: I, filemtime ($ upload_dir. $ file ))
. </Font> </small> </sub>;
$ Filelist. = <br>;
}
}

Function do_upload ($ upload_dir, $ upload_url ){

$ Temp_name = $ _ FILES ['userfile'] ['tmp _ name'];
$ File_name = $ _ FILES ['userfile'] ['name'];
$ File_name = str_replace (\\\\,, $ file_name );
$ File_name = str_replace (', $ file_name );
$ File_path = $ upload_dir. $ file_name;

// File Name Check
If ($ file_name = ){
$ Message = Invalid File Name Specified;
Return $ message;
}

$ Result = move_uploaded_file ($ temp_name, $ file_path );
If (! Chmod ($ file_path, 0777 ))
$ Message = change permission to 777 failed .;
Else
$ Message = ($ result )? $ File_name uploaded successfully .:
Somthing is wrong with uploading a file .;
Return $ message;
}

?>

<Center>
<Font color = red> <? =_ _ REQUEST [message]?> </Font>
<Br>
<Form name = upload id = upload ENCTYPE = multipart/form-data method = post>
Upload File <input type = file id = userfile name = userfile>
<Input type = submit name = upload value = Upload>
</Form>

<Br> <B> My Files </B>
<Hr width = 70%>
<? = $ Filelist?>
<Hr width = 70%>
<Small> <sup> Developed
<A style = text-decoration: none href = http://tech.citypost.ca> CityPost. ca </a>
</Sup> </small>
</Center>
 

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.