Answer the question
In order to leave comments, you need to log in
Console controller access denied, how to solve?
Good afternoon.
It is necessary to process uploaded images via cron.
Created such a console controller.
class ImagesController extends Controller
{
public function actionResize()
{
Yii::setAlias('@web', dirname(__DIR__) . '/web');
$path = Yii::getAlias('@web') . '/uploads';
$images = $this->scanDir($path); // рекурсивный обход директорий
try{
foreach ($images as $image){
$img = Image::getImagine()->open($path . '/' . $image);
//echo $path . '/' . $image . PHP_EOL;
if($img->getSize()->getWidth() > 1000){
Image::resize($path . '/' . $image, 1000, null)->save($path. '/' . $image, ['jpeg_quality' => 80]);
echo 'Done!' . PHP_EOL;
}
}
}
catch (Exception $exception){
Yii::getLogger()->log($exception, Logger::LEVEL_WARNING);
Yii::getLogger()->log($exception, Logger::LEVEL_ERROR);
echo 'Error' . PHP_EOL;
}
}
}
ImagickException: unable to open image `/home/slonik/localhost/www/memory/web/uploads/country_1/region_5/city_5/JPJvU8GlmulD-KCHdlwBKSHHcVHhBWpk.jpg': Permission denied @ error/blob.c/OpenBlob/2712 in /home /slonik/localhost/www/memory/vendor/imagine/imagine/lib/Imagine/Imagick/Image.php:287
if(!file_exists($p)){
mkdir($p, 0777, true);
};
drwxrwxrwx 1 slonik slonik 18 Сен 26 18:05 uploads
drwxr-xr-x 1 www-data www-data 16 Сен 26 18:05 country_1
drwxr-xr-x 1 www-data www-data 12 Сен 26 18:05 region_5
drwxr-xr-x 1 www-data www-data 20 Сен 26 18:05 city_5
-rw-r--r-- 1 www-data www-data 845155 Сен 26 18:05 1g62QFwjgt67kwUcMEV3DcAViJHlAmUN.jpg
-rw-r--r-- 1 www-data www-data 878004 Сен 26 18:05 4HSV4UdL3gWFC5UKNmcYSgDJmYQODj6V.jpg
-rw-r--r-- 1 www-data www-data 824929 Сен 26 18:05 C9asvh5KiEvpOvbkh8SXXyVLzzCQyenr.jpg
Answer the question
In order to leave comments, you need to log in
The issue was resolved. They told me how to do it right.
The apache2 mpm-itk module is required .
This module allows you to run virtual hosts as a user. Among other things, this allows you not to think about setting additional rights to certain folders and files on your sites.
I have Ubuntu 16.04., apache2 server.
1) Check if apache2 is built with mpm-itk support
in the console
, as a result, look for the line mpm_itk_module (static)
2) If not, then install the module.
3) Edit the virtual host configuration, enter the following instruction there
<IfModule mpm_itk_module>
AssignUserId your_user_name your_user_groupe
</IfModule>
The problem I think is that the web application is launched from the user under which apache or php-fpm is running, and the console application from the one who runs the cron, either you or yourself.
And of course, if these files are created from a web application, then by default they are not available for changes to other users.
from the advice, either save images initially with rights 0777 or add a user who runs cron to the apache group, for example, if it is ubuntu, then usually it is www-data
Somehow I came across that the command
mkdir ($ p, 0777, true);
Doesn't set the right permissions below added chmod
You can
try sudo usermod -aG www-data YOU_USER
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question