I have a yii controller that creating folder and downloading images in it with curl.
If I run script from browser it works like I needed, all good, but if I try to run this script from the terminal through yii console application I get this errors: mkdir error img error
I tried to give chmod 777
permissions and chown www-data
to entire project folder but it doesn't helped
Here is my functions that downloading images:
public function loadImg($id_tovar, $id_post, $file)
{
if (strpos($file, 'swf') === false) {
ini_set('memory_limit', '-1');
set_time_limit(0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$img = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
$err = curl_errno( $ch );
curl_close($ch);
if (@imagecreatefromstring($img) !== false) {
$source_image = imagecreatefromstring($img);
}
if (isset($source_image)) {
$exPos = strrpos($file, '.');
$type = substr($file, $exPos);
$filename = 'tov_'.$id_tovar.'_'.substr(md5(uniqid(mt_rand(), true)), 0, 8).$type;
$dirFull = $_SERVER['DOCUMENT_ROOT'].'/nal/img/'.$id_post;
if(!is_dir($dirFull)){
mkdir($dirFull);
}
$fullPath = $dirFull . '/'. $filename;
self::resizeImage($source_image, $type, $filename, $dirFull);
self::resizeImage($source_image, $type, $filename, $dirFull, 280, 280, '/l_', true);
return 'ok';
}
}
return false;
}
public static function resizeImage($source, $tip, $filename, $root, $w = 1080, $h = 1140, $pref = '/b_', $delImg = false){
$max_shir = $w;
$max_vis = $h;
$w_src = imagesx($source);
$h_src = imagesy($source);
$kw = $max_shir/$w_src;
$kh = $max_vis/$h_src;
$k = $kw;
if($kw > $kh){
$k = $kh;
}
if($k>1){
$k = 1;
}
$shir = (int)($w_src * $k);
$vis = (int)($h_src * $k);
$smesh_x = (int) (($max_shir - $shir)/2);
$smesh_y = (int) (($max_vis - $vis)/2);
$dest = imagecreatetruecolor($max_shir, $max_vis);
imageAlphaBlending($dest, false);
imageSaveAlpha($dest, true);
$color = imagecolorallocate ( $dest , 255 , 255 , 255 );
imagefill ( $dest, 0 , 0 , $color );
imagecopyresampled($dest, $source, $smesh_x, $smesh_y, 0, 0, $shir, $vis, $w_src, $h_src);
$PUTH = $root;
$name=$PUTH.$pref.$filename;
if($tip == '.jpg' || $tip == '.jpeg' || $tip == '.JPG'){
imagejpeg($dest, $name, 100);
}
if($tip == '.png'){
imagepng($dest, $name, 8);
}
if($delImg){
imagedestroy($dest);
imagedestroy($source);
}
}
I'm new to the programming world, so i need help of big guys. Thx a lot)
When you are running your script using cli instead of web server there is no document root, so $_SERVER['DOCUMENT_ROOT']
is empty and you end up with absolute path like this /nal/img/...
which is wrong.
You can try to use something like this to get absolute path to root folder:
$dirFull = dirname(__DIR__) .'/nal/img/'.$id_post;
Magic const __DIR__
points to directory of current script and dirname()
function basically gives you path one folder above path given in its argument. So you might need to modify the number of times dirname()
function is called as needed to match your file structure.
Or you can use Yii's aliases to get correct absolute path:
$dirFull = \Yii::getAlias('@app/nal/img/' . $id_post);