2017-08-26 62 views
1

我試圖使用Resumable Uploads模式實現ng-file-upload,以分割大塊文件並在上載後合併它們。我在很多項目中實現了ng-file-upload,但這是我第一次上傳這麼大的文件。使用ng-file-upload + PHP(合併部分)可恢復上載

我的問題是,我不知道如何使它在PHP中的服務器端文件。我只是需要用不同的名稱上傳大塊,但我無法合併它們。

任何人都可以發佈一個PHP的服務器端代碼的例子,使這個功能工作?

這是我做了這一點:

AngularJS

$scope.uploadMediaFile = function (file) { 

     if(file) { 

      Upload.upload({ 
       ignoreLoadingBar: true, 
       url: 'app/api/upload/mediaFile.php', 
       resumeChunkSize: '1MB', 
       file: file 
      }).then(function (response) { 
       if(response.data.success) { 
        $scope.post.mediaFile = response.data.filename; 
        $scope.post.duration = response.data.duration; 
       } else { 
        console.error(response.data.error); 
       } 
      }, null, function (evt) { 
       console.log(part); 
       file.progress = Math.min(100, parseInt(100.0 * evt.loaded/evt.total)); 
      }); 
     } 
    }; 

mediaFile.php

$filename = $_FILES['file']['name']; 
$file_tmp = $_FILES['file']['tmp_name']; 

$file_ext = pathinfo($filename, PATHINFO_EXTENSION); 

$file_des = $_SERVER['DOCUMENT_ROOT'] . '/storage/content/temp/'; 

if(!file_exists($file_des)) mkdir($file_des); 

// Puting a diferent name for each file part 
$new_filename = uniqid() . "." . pathinfo($filename, PATHINFO_EXTENSION); 

move_uploaded_file($file_tmp, $file_des . $new_filename) 

到目前爲止,我得到相同文件的許多作品與diferent名。

回答

1

爲了防止有人看到類似的問題,我張貼我的解決方案。

<?php 
// File chunk 
$filename = $_FILES['file']['name']; 
$file_tmp = $_FILES['file']['tmp_name']; 


// Defining temporary directory 

$file_des = $_SERVER['DOCUMENT_ROOT'] . '/storage/content/temp/'; 

// If not exists, create temp dir. 
if(!file_exists($file_des)) mkdir($file_des); 

// The first chunk have the original name of file uploaded 
// so, if it exists in temp dir, upload the other pieces 
// with anothers uniques names 
if(file_exists($file_des . $filename)) { 

    $new_name = uniqid() . "." . pathinfo($filename, PATHINFO_EXTENSION); 
    move_uploaded_file($file_tmp, $file_des . $new_name); 

    // Now, append the chunk file to the first base file. 
    $handle = fopen($file_des . $new_name, 'rb'); 
    $buff = fread($handle, filesize($file_des . $new_name)); 
    fclose($handle); 

    $final = fopen($file_des . $filename, 'ab'); 
    $write = fwrite($final, $buff); 
    fclose($final); 

    // Delete chunk 
    unlink($file_des . $new_name); 

} else { 

    /* MAKE SURE WE DELETE THE CONTENT OF THE DESTINATION FOLDER FIRST, 
     OTHERWISE CHUNKS WILL BE APPENDED FOR EVER 
     IN CASE YOU ARE TRYING TO UPLOAD A FILE WITH THE EXACT SAME NAME. 
     CAREFUL: YOU MAY PREFER TO DELETE ONLY THE FILE 
     INSTEAD OF THE FOLDER'S CONTENT, IN THE CASE 
     YOUR FOLDER CONTAINS MORE THAN ONE FILE. 
    */ 

    $files_to_delete = glob($file_des."*"); // get all file names 
    foreach($files_to_delete as $file) // iterate files 
    { 
     if(is_file($file)) 
     { 
     unlink($file); // delete file 
     } 
    } 


    // First chunk of file with original name. 
    move_uploaded_file($file_tmp, $file_des . $filename); 
}