【问题标题】:Yii resumable implementingYii 可恢复实现
【发布时间】:2014-05-31 14:05:49
【问题描述】:

我有一个任务要在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。我尝试使用休息服务发送该文件。

【问题讨论】:

  • 那么,您需要怎样的帮助。您的帖子不清楚。
  • resumablejs.com 嗨,这是可恢复的,但我不知道如何在控制器中使用它,谢谢您的帮助。我将首先尝试解决该问题,然后我将尝试实施可恢复的。

标签: php file-upload yii resume-upload resumablejs


【解决方案1】:

Sample server implementation in 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);
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多