How to upload multiple files in PHP using APC _ PHP Tutorial

Source: Internet
Author: User
Tags apc php file upload
The APC method analyzes the principle of uploading multiple files in PHP. It may take a long wait time to upload multiple files. due to the impact of the network and software, the upload may often fail due to timeout. It may take a long time to upload multiple files in PHP. due to the influence of the network and software, the upload may often fail. How can I get the file upload progress in real time when I upload multiple files in PHP, and how can I avoid restrictions on php and other factors to upload files of any size?

  • Experience: sample PHP file upload code
  • 5-minute PHP file upload code demonstration
  • Mining PHP file Upload principles
  • Three-step FTP implementation PHP file upload code analysis
  • Two PHP file size restrictions
Php is different from other languages such as asp in processing uploaded files. in asp, you can use Request. BinaryRead to stream the data submitted by the client. Php stores a file in a temporary folder. after the file is uploaded, you can obtain its information and perform operations on it. If we can get the file name of the temporary file during the upload process, we can determine the size of the temporary file to get the upload progress, but it seems that there is no way to get it. Therefore, we can only create a simple server by using the socket extension of php, post the data to the server, and then use our own mechanism for processing.

Let's talk about the process. First, use the socket function library of php to create a temporary http server, listen on a port, and then notify the client of the IP address and port number, the client submits the upload form (temporary server), the temporary server accepts the client request, reads the post data, analyzes and obtains the file information uploaded by the client, and saves the file on the server, disable the temporary server, release the resource, and the upload is complete. It's a bit difficult, but the idea is still simple.

Later I will release a class library named UGiA Visual PHP Uploader, so that you can easily call it in the program. PHP uploads multiple files as follows:

APC implementation for uploading multiple files in PHP:

Install APC by referring to the official documentation. you can use the PECL module to install APC quickly and easily. ini, set the apc parameter. rfc1867 = 1 to enable APC to support the upload progress bar function. the following code examples are provided in the APC source code instructions:

 
 
  1. If ($ _ SERVER ['request _ method'] = 'post') {// upload REQUEST
  2. $Status=Apc_fetch('Upload _ '. $ _ POST ['apc _ UPLOAD_PROGRESS']);
  3. $ Status ['Done'] = 1;
  4. Echo json_encode ($ status); // ajax call output to the user's page. please find the relevant documentation
  5. Exit;
  6. } Elseif (isset ($ _ GET ['SS SS _ key']) {// read the upload progress
  7. $Status=Apc_fetch('Upload _ '. $ _ GET ['progress _ key']);
  8. Echo json_encode ($ status );
  9. Exit;
  10. } Else {
  11. // Other code, such as uploading a form
  12. }

Uploadprogress module implementation method:

 
 
  1. Use the PECL module installation method to install the settings in php. ini of this moduleUploadprogress. file. filename_template= "/Tmp/upd_%s.txt" code example:
  2. If ($ _ SERVER ['request _ method'] = 'post '){
  3. If (is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
  4. $Upload_dir='Your _ path /';
  5. $Ext=Strrchr($ _ FILES ['video'] ['name'], '.');
  6. $Sessid= $ _ POST ['upload _ identifier'];
  7. $Tmpfile= $ Upload_dir. $ sessid;
  8. $Sessfile= $ Upload_dir. $ sessid. $ ext;
  9. If (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ tmpfile )){
  10. // Upload successful
  11. } Else {
  12. // Upload failed
  13. } Else {
  14. // Upload error
  15. } Elseif (! Empty ($ _ GET ['sessid ']) {
  16. Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
  17. Header ("Last-Modified:". gmdate ("D, d m y h: I: s"). "GMT ");
  18. Header ("Cache-Control: no-store, no-cache, must-revalidate ");
  19. Header ("Cache-Control:Post-check=0,Pre-check=0", False );
  20. Header ("Pragma: no-cache ");
  21. Header ("Content-Type: text/html;Charset=UTF-8 ");
  22. $Unique_id= $ _ GET ['sessid '];
  23. $Uploadvalues=Uploadprogress_get_info($ Unique_id );
  24. If (is_array ($ uploadvalues )){
  25. Echo json_encode ($ uploadvalues );
  26. } Else {
  27. // Read progress failed. additional processing logic
  28. }
  29. } Else {
  30. // Display the upload form

Bytes. How to upload files in PHP...

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.