0

我有一个任务要在Yii中实现 resumable ,并且我实现了上传控制,但以前从未 Resumable。

public function actionUpload()
    {
        $model=new User;
        if(isset($_POST['User'])) {
             $model->attributes=$_POST['User'];
             $model->image=CUploadedFile::getInstance($model,'image');
             if($model->save()) {
                 $model->image->saveAs('upload/'.$model->image->name);
                 $this->redirect(array('view','id'=>$model->uUserID));
             }
        }
        $this->render('upload',array('model'=>$model));
    }

任务是将文件分成小块。

示例:一个文件可以是 1 GB。我尝试使用休息服务发送该文件。

4

1 回答 1

0

请参阅PHP 中的示例服务器实现

我在此处复制粘贴该页面上提供的代码的重要部分:

/**
 *
 * Check if all the parts exist, and 
 * gather all the parts of the file together
 * @param string $dir - the temporary directory holding all the parts of the file
 * @param string $fileName - the original file name
 * @param string $chunkSize - each chunk size (in bytes)
 * @param string $totalSize - original file size (in bytes)
 */
function createFileFromChunks($temp_dir, $fileName, $chunkSize, $totalSize) {

    // count all the parts of this file
    $total_files = 0;
    foreach(scandir($temp_dir) as $file) {
        if (stripos($file, $fileName) !== false) {
            $total_files++;
        }
    }

    // check that all the parts are present
    // the size of the last part is between chunkSize and 2*$chunkSize
    if ($total_files * $chunkSize >=  ($totalSize - $chunkSize + 1)) {

        // create the final destination file 
        if (($fp = fopen('temp/'.$fileName, 'w')) !== false) {
            for ($i=1; $i<=$total_files; $i++) {
                fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
                _log('writing chunk '.$i);
            }
            fclose($fp);
        } else {
            _log('cannot create the destination file');
            return false;
        }

        // rename the temporary directory (to avoid access from other 
        // concurrent chunks uploads) and than delete it
        if (rename($temp_dir, $temp_dir.'_UNUSED')) {
            rrmdir($temp_dir.'_UNUSED');
        } else {
            rrmdir($temp_dir);
        }
    }

}
于 2014-12-01T16:30:37.610 回答