M
M
Maxim Timofeev2016-09-05 20:37:41
Yii
Maxim Timofeev, 2016-09-05 20:37:41

How to properly create a virtual link in php?

Now I've done it like this:

public function actionFile($file){
        $path = '';
        if(Yii::$app->user->can('adminka')){
            if($doc = Docs::find()->where(['file_name'=>$file])->one()){
                $path = Yii::getAlias('@common').'/docs/'.$doc->user_id.'/'.$doc->file_name;
            }
        }else{
            if($doc = Docs::find()->andWhere(['file_name'=>$file])->andWhere(['user_id'=>Yii::$app->user->id])->one()){
                $path = Yii::getAlias('@common').'/docs/'.$doc->user_id.'/'.$doc->file_name;
            }else{
                throw new yii\web\ForbiddenHttpException;
            }
        }
        if(!empty($path)){
            $response = Yii::$app->response;
            $response->format = \yii\web\Response::FORMAT_RAW;
            $mime = yii\helpers\FileHelper::getMimeType($path);
            header('Content-type: '.$mime);
            echo file_get_contents($path);
        }else{
            throw new NotFoundHttpException;
        }
    }

The link looks like domen/file/file_name.file_extention
Everything works, but not sure if this is the sane way. And the most important question is "whether the server will die with a file size of 100mb or 1GB"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-09-05
@webinar

file_get_contents??? If nginx is missing and there is no way to install (x-accel-redirect), then at least make reading / uploading in blocks of 1-10 kb. On large files, there will not be enough resume.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question