You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yii控制器脚本浏览器运行正常,控制台执行报错求助

问题:Yii控制台执行图片下载脚本时出现mkdir和图片处理错误

我有一个Yii控制器,通过curl创建文件夹并下载图片。从浏览器运行脚本时一切正常,但通过Yii控制台应用在终端执行时,出现mkdir错误和图片处理错误。我尝试给整个项目文件夹设置chmod 777权限和chown www-data,但没有效果。

以下是用于下载图片的函数:

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);

    }

}

内容的提问来源于stack exchange,提问作者riskindness

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 06:47:18