The concrete idea of uploading with PHP file and implementing _php Tutorial

Source: Internet
Author: User
File upload we need to use the Type=file type of the form inside the HTML, and its Enctype property. 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]\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,.));

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 being 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>

http://www.bkjia.com/PHPjc/508470.html www.bkjia.com true http://www.bkjia.com/PHPjc/508470.html techarticle file Upload We need to use the Type=file type of the form inside the HTML, and its Enctype property. This is what we all have to use. Of course, PHP function library in the File function library, String class ...

  • 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.