file upload We need to use the type=file type of the form in HTML, and its enctype properties. 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 each site may have a lot of restrictions on uploading files, these restrictions will include file types, file size, extension, and the existence of the upload directory, upload files exist or not, the directory is writable, readable, Upload the file name and how to copy the file from the cache to the directory you want.
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> |