@@ -205,9 +205,11 @@ function ppom_upload_file() {
205
205
$ file_path_thumb = $ file_dir_path . 'thumbs ' ;
206
206
$ file_name = wp_unique_filename ( $ file_path_thumb , $ file_name );
207
207
$ file_name = strtolower ( $ file_name );
208
+ $ original_name = $ file_name ;
208
209
$ file_ext = pathinfo ( $ file_name , PATHINFO_EXTENSION );
210
+ $ original_name = str_replace (". $ file_ext " , "" , $ original_name );
209
211
$ unique_hash = substr ( hash ( 'sha256 ' , wp_generate_password ( 8 , false , false ) ), 0 , 8 );
210
- $ file_name = str_replace ( ". $ file_ext " , ". $ unique_hash. $ file_ext " , $ file_name );
212
+ $ file_name = str_replace ( ". $ file_ext " , ". " . $ chunk . " . $ unique_hash. $ file_ext " , $ file_name );
211
213
$ file_path = $ file_dir_path . $ file_name ;
212
214
213
215
// Make sure the fileName is unique but only if chunking is disabled
@@ -299,7 +301,45 @@ function ppom_upload_file() {
299
301
}
300
302
301
303
// Check if file has been uploaded
302
- if ( ! $ chunks || $ chunk == $ chunks - 1 ) {
304
+ if ( ! $ chunks || $ chunk === $ chunks - 1 ) {
305
+
306
+ // Gather all the chunks and combine them into a single file.
307
+ $ final_file = fopen ($ file_path , 'wb ' );
308
+ if ( $ final_file ) {
309
+ $ chunks_files_path = [];
310
+ $ dir = opendir ($ file_dir_path );
311
+ if ($ dir ) {
312
+ while (($ file = readdir ($ dir )) !== false ) {
313
+ $ tmpfilePath = $ file_dir_path . $ file ;
314
+
315
+ if ( false !== strpos ($ tmpfilePath , $ original_name ) && false !== strpos ($ tmpfilePath , '.part ' )) {
316
+ $ chunks_files_path [] = $ tmpfilePath ;
317
+ }
318
+ }
319
+ closedir ($ dir );
320
+ }
321
+
322
+ sort ( $ chunks_files_path );
323
+
324
+ foreach ( $ chunks_files_path as $ tmpfilePath ) {
325
+ $ chunk_file = fopen ($ tmpfilePath , 'rb ' );
326
+ if ($ chunk_file ) {
327
+ while ($ buffer = fread ($ chunk_file , 4096 )) {
328
+ fwrite ($ final_file , $ buffer );
329
+ }
330
+ fclose ($ chunk_file );
331
+ // Optionally, you can delete the chunk file after appending it to the final file
332
+ unlink ($ tmpfilePath );
333
+ } else {
334
+ die ('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to open chunk file."}, "id" : "id"} ' );
335
+ }
336
+ }
337
+
338
+ fclose ($ final_file );
339
+ } else {
340
+ die ('{"jsonrpc" : "2.0", "error" : {"code": 104, "message": "Failed to open final file."}, "id" : "id"} ' );
341
+ }
342
+
303
343
// Strip the temp .part suffix off
304
344
rename ( "{$ file_path }.part " , $ file_path );
305
345
0 commit comments