yii2创建新文件夹并向其中添加文件


yii2 Create new folder and add file to it

我正在创建Yii2应用程序,它允许用户下载教程及其副标题。目前我可以单独做,但我想做的是创建一个带有视频名称的新文件夹,并在该文件夹中包含视频和字幕文件,然后创建一个zip并将其提供给用户下载。但是我不知道怎么做。我可以使用php-ZipArchive压缩文件,但我不知道如何创建新的文件夹并将这两个文件包括在内。

我的下载操作是

public function actionDownload($id)
{
    // get all videos relevan for this video id 
    $video = TempVideo::findOne($id);
    $file = $video->path;
        if (file_exists($file)) 
        {
              Yii::$app->response->sendFile($file);
        } 
}

我们非常感谢您的帮助。

您可以使用ZipArchivehttp://php.net/manual/en/zip.examples.php

或启动命令行:

system('zip filecompress.zip file1 file2 file3');

我没有所有的信息,但我认为它需要mlook这样的东西:您需要使用mkdir()(文档:http://php.net/manual/en/function.mkdir.php);并使用copy()复制所需的文件(文件:http://php.net/manual/en/function.copy.php)

public function actionDownload($id)
{
    // get all videos relevan for this video id 
    $video = TempVideo::findOne($id);
    $file = $video->path;
        if (file_exists($file)) 
        {
              Yii::$app->response->sendFile($file);
              $new_video_path = '/path/to/save/dir';
              //create a new dir
              mkdir($new_video_path); 
              //copy the file to a new path
              copy($file,$new_video_path.'newfilename.mp4');
              copy('subtitle/pathe.txt',$new_video_path.'subtitle.txt');
        } 
}

然后你可以用ZipArchive创建一个zip文件,文档可以在这里找到:http://php.net/manual/en/class.ziparchive.php