PHP File upload specific ideas and implementation of _php tutorial

Source: Internet
Author: User
Tags php file upload
The recent period of time on the PHP file processing is very interesting, so many sites have seen a lot of file processing articles, but many of the domestic site of the PHP file processing knowledge most of you copy me I copy you, The baidu.com or google.com search is repeated. Recently in a foreign site on the shield of an article feel very good, so recommend to everyone to read.

First of all, we need to explain the file upload process and the use of knowledge points:

File upload we need to use the type= "file" type, and its Enctype property, in the HTML form. This is what we all have to use. Of course, the use of the file function library, the string type function library, the directory function library and the $_files[] in the PHP library is what we have to do.

Perhaps each site may have many restrictions on the uploading of files, which will include the file type, file size, extension, and the presence or absence of the upload directory, upload the file exists or not, the directory is writable, readable, upload the file name and how to copy the file from the cache to the directory you need.

Of course, we can not ignore the preprocessing of errors! If we take another step further, we also have the ability to record the event log for the operation of the file.

Here's a procedure for implementing these features:

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


The first is our default 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;


The next is to 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 = "";


Set up the upload directory and change the permissions accordingly:

if (!is_dir ("files")) {
if (!mkdir ($upload _dir))
Die ("upload_files directory doesnt 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", "a");
Fwrite ($resource, Date ("Ymd h:i:s"). " DELETE-$_SERVER[REMOTE_ADDR] "." $_request[del] ");
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 "";
}
}
else if ($_files[userfile]) {
$resource = fopen ("Log.txt", "a");
Fwrite ($resource, Date ("Ymd h:i:s"). " UPLOAD-$_SERVER[REMOTE_ADDR] "
. $_files[userfile][name]. " "
. $_files[userfile][type]. "");
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 2MB.";
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 "";
}
else if (!$_files[userfile]);
Else
$message = "Invalid File Specified.";

List the files we uploaded:

$handle =opendir ($upload _dir);
$filelist = "";
while ($file = Readdir ($handle)) {
if (!is_dir ($file) &&!is_link ($file)) {
$filelist. = "". $file. "";
if ($DELETABLE)
$filelist. = "X";
$filelist. = " ". Date (" D-m h:i ", Filemtime ($upload _dir. $file))
."
";
$filelist. = "
";
}
}

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;
}

?>








My Files



Developed by
citypost.ca



http://www.bkjia.com/PHPjc/532625.html www.bkjia.com true http://www.bkjia.com/PHPjc/532625.html techarticle The recent period of time on the PHP file processing is very interesting, so many sites have seen a lot of file processing articles, but many of the domestic site of PHP file processing knowledge ...

  • Related Article

    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.