PHP implements file upload and ThinkPHP upload Extension

Source: Internet
Author: User
PHP implements the file upload function. The file upload function is a common feature in web development. aopli also encountered this problem during the internship, but it was previously developed using asp and jsp, it is troublesome to upload files using asp and jsp, but it is relatively simple in php. The following is a brief introduction to the Code: first, I implemented the upload of xls files,

PHP implements the file upload function. The file upload function is a common feature in web development. aopli also encountered this problem during the internship, but it was previously developed using asp and jsp, it is troublesome to upload files using asp and jsp, but it is relatively simple in php. The following is a brief introduction to the Code: first, I implemented the upload of xls files,

PHP implements File Upload

The file upload function is a common feature in web development. aopli encountered such problems during internships, but it was previously developed using asp and jsp, it is troublesome to upload files using asp and jsp, but it is relatively simple in php. The following is a brief introduction to the Code:

First, I implemented the upload of the xls file, which can be changed as needed. First, I added enctype = "multipart/form-data" to the form. I want to solve this problem, I should not have to repeat it again, and then add a file domain. Here I name it file, as shown below:And then submit it to the processing page. The following is the processing code:

The name must be:

Obtained parameters:

? $ _ FILES ["file"] ["name"]-name of the uploaded file

? $ _ FILES ["file"] ["type"]-type of the file to be uploaded

? $ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes

? $ _ FILES ["file"] ["tmp_name"]-name of the temporary copy of the file stored on the server

? $ _ FILES ["file"] ["error"]-error code caused by file Upload

This is all the parameters, which can be changed according to your own requirements. For example, if you need to upload a doc file, change the type. The first parameter file is the name of the file field.

If (! Empty ($ _ FILES ['file'] ['name'])

{

$ Tmp_file = $ _ FILES ['file'] ['tmp _ name'];

$ File_types = explode (".", $ _ FILES ['file'] ['name']);

$ File_type = $ file_types [count ($ file_types)-1];

If (strtolower ($ file_type )! = "Xls ")

{

Echo ("not an Excel file, re-upload ");

}

$ SavePath = 'public/upload/excel /';

$ Str = date ('ymdhis ');

$ File_name = $ str. ".". $ file_type;

If (! Copy ($ tmp_file, $ savePath. $ file_name ))

{

Echo ("failed ");

}

Else

{

Echo ($ savePath. $ file_name );

Echo ("success
");

}

}

The last step is to set the path correctly. If success occurs, check the directory on the server. The upload is successful, but not necessarily the directory you want to upload.


The following are technical extensions. I think many commercial projects will be applied to the PHP framework. The following are some extensions I have collected. I hope they can help you...

For more information, see:

1. PHP implements File Upload

1.1.http: // www.w3school.com.cn/php/php_file_upload.asp

Technical expansion:

2. ThinkPHP implements File Upload

2.1.ThinkPHP official manual: http://doc.thinkphp1.cn/manual/upload.html

2.2. ThinkPHP file upload: http://www.oschina.net/code/snippet_783422_18835

2.3.ThinkPHP file upload: http://blog.thinkwap.com/1159

. How to use Baidu space-ThinkPHP upload Class 2:

Http://hi.baidu.com/rx1987/item/15ab4e1d15f5ed633f87ce9f

2.5. How to Use the ThinkPHP File Upload class UploadFile. class. php:

Http://digdeeply.org/archives/03261917.html

2.6. ThinkPHP3.1 implement File Upload (Multi-File Upload is supported ):

Http://www.111cn.net/phper/thinkPhp/46362.htm

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.