code idea
When PHP uploads very large files, you can’t just let the backend php upload , and it will easily lose half of the benefits of internal testing. The front end needs to cooperate with the back end to process together, and the file upload should use the AJAX method instead of the submit method of the form.
The front end divides the file object into files of a certain size according to a certain size (such as 2M or 5M), and uploads the divided files to the backend one by one. After the backend receives the fragmented files, the They are first placed in a temporary directory. When the data request completed by the front end is received, the files in the temporary directory are assembled into a new file. After saving, delete the files in the temporary directory.
sample code
HTML code:
1 2 3 |
|
Here to explain, do not use submit to upload, use ajax to upload.
JS code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
The above is the core part of the front-end js, and the comments can basically be understood.
php code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
The above method, I uploaded a 650M file locally, and it only took 20 seconds. I haven’t tested it on the server. You can try this method.