How can I view images that are stored in a root folder?

I am taking an old project of the company and I have the following problem, the images uploaded by the users and displayed on the platform are saved in an img folder but in the root of the project.

The reason why they were in the root folder of the project, was because there was no web file so all the .htaccess and index.php were left in the root also as single files.

My question is there a way to continue using the root images without having to move them to the web folder since it is supposed to be a safe place where they are stored because they can be images that I do not want someone external can inspect and see all the documents and images that are uploaded in the app.

Since I created the web folder and I was structuring it to what is the real Yii2 structure, those images are no longer visible. If I leave the project as it was originally, I don’t have any display problems.

1 Like

Have you considered symlinks?

I didn’t know that Yii2 had simblinks too, I know how to do it in Laravel.

The thing is that they gave me this project without knowing anything about Yii2 to correct it. I searched the internet and tried the alias(‘web’) and everything you explain but I don’t know where those simblinks are generated or in which part of the code they are placed.

Symlinks are Platform specific things and have nothing to do with Yii or Laravel.
Try them out and see

1 Like

You can write controller action that accepts filePath, looks for it in images dir, gets image contents and displays it with corresponding content type

1 Like

But if I load a lot of images on the website won’t it be too much load on the controller?

reading images is not a problem. Try out and see for your self. Code should be similar to:

$image = 'my_img.jpeg';
$imageFile = Yii::getAlias("@app/images/{$image}");
$imageExt = pathinfo($imageFile, PATHINFO_EXTENSION)

$response->headers->set('Content-Type', "image/{$imageExt}");
$response->format = Response::FORMAT_RAW;

return is_file($imageFile) 
    ? file_get_content($imageFile)
    : "";
1 Like