Upload
The recent period of time on the PHP file processing is very interesting, so many sites to see a lot of document processing articles, but many of the domestic site of the PHP file processing knowledge most of you copy me I copy you, The search for baidu.com or google.com is more repetitive. Recently in a foreign site on the shield an article feel very good, so recommended to everyone to read.
First of all, we need to explain the file upload operation flow and the use of knowledge points:
File upload we need to use the type= "file" type of the form in HTML, and its enctype attribute. This is what we all have to use. Of course, the use of the file function library, the string type function library, the catalog function library and the $_files[] in the PHP function library is what we have to use.
Perhaps every site may have a lot of restrictions on uploading files, these restrictions will include file type, file size, extension, and the presence of the uploaded directory, the presence or absence of the uploaded file, the readability of the directory, the legibility of the uploaded file, and how to copy the file from the cache to the directory you need.
Of course, the preprocessing of error is also we can not ignore! If we have a further discussion, we also have the record of the event log used for the operation of the file.
Here's a procedure to implement these features:
--------------------------------------------------------------------------------------------
The first is our preset variable value, which includes file size, file name extension type, Mimi type, and whether or not 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 one is to set 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 the upload directory and change the permissions 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", "a");
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", "a");
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, "."));
File size check:
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 isn't 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 the files we uploaded:
$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><small><font color=grey>". Date ("D-m h:i", Filemtime ($upload _dir.$ file))
." </font></small></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 by
<a style= "Text-decoration:none" href= "http://tech.citypost.ca" >CityPost.ca</a>
</sup></small>
</center>