上传路径在图片上传代码中似乎无效

时间:2019-02-12 11:55:27

标签: php codeigniter

每当我运行此代码时,每次在图片上传代码中上传路径似乎无效时,它就会显示给我。

im在代码初始化器中是新的。 请帮助解决此问题。 谢谢。

function uploadImage($name,$folder)
    {
        $response = array();

      //  $config['upload_path'] = base_url() . "uploads/demo/image/".$folder."/";
        $config['upload_path'] = base_url() . "./image/".$folder."/";

        $config["allowed_types"] = 'gif|jpg|png|jpeg';
        $config["encrypt_name"] = true;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if($this->upload->do_upload($name))
        {
            $data = $this->upload->data();
            $config3 = array(
                'source_image' =>$data['full_path'],
                'new_image' => './image/'.$folder.'/thumb/',
                'maintain_ratio' => true,
                'width' => 150,
                'height' => 100
            );
            $this->load->library('image_lib', $config3);
            $this->image_lib->resize();
            $name = $data['file_name'];

            $response['flag'] = "true";
            $response['name'] = $name;

        }
        else
        {
            $response['flag'] = "false";
            $response['name'] = $this->upload->display_errors();
        }
        print_r($response);
        return $response;
    }

3 个答案:

答案 0 :(得分:3)

函数uploadImage($ name,$ folder)     {         $ response = array();

  //  $config['upload_path'] = base_url() . "uploads/demo/image/".$folder."/";
    $config['upload_path'] = base_url() . "./image/".$folder."/";

    $config["allowed_types"] = 'gif|jpg|png|jpeg';
    $config["encrypt_name"] = true;
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if($this->upload->do_upload($name))
    {
        $data = $this->upload->data();
        $config3 = array(
            'source_image' =>$data['full_path'],
            'new_image' => './image/'.$folder.'/thumb/',
            'maintain_ratio' => true,
            'width' => 150,
            'height' => 100
        );
        $this->load->library('image_lib', $config3);
        $this->image_lib->resize();
        $name = $data['file_name'];

        $response['flag'] = "true";
        $response['name'] = $name;

    }
    else
    {
        $response['flag'] = "false";
        $response['name'] = $this->upload->display_errors();
    }
    print_r($response);
    return $response;
}

答案 1 :(得分:0)

将此添加到config / config.php文件中

$config['base_path']    = realpath(dirname(__FILE__).'/../../');
define('BASE_PATH',$config['base_path']);

之后 使用这个

$config['upload_path'] = BASE_PATH . "/image/".$folder."/"; //C:\wamp\www\ , absolute path

代替此

$config['upload_path'] = base_url() . "/image/".$folder."/";//http://localhost/ ,relative path

答案 2 :(得分:0)

您有错字:

  

$ config ['upload_path'] = base_url()。 “ ./image /".$ folder。” /“;

应为$config['upload_path'] = base_url("image/$folder/");

还应注意,当您在共享服务器上并且需要寻址相对路径时(在服务器的主目录中,可以在cPanel主页上找到该错误),也会出现该错误。

我发现以下方法可以解析本地主机和共享服务器的上传路径:

$url = "./image/$folder/";  // LOCALHOST
$cur_url = $_SERVER["HTTP_HOST"]; //=='www.mydomain.com')

if (strpos($cur_url, 'mydomain.com') !== false) {
  // PRODUCTION
  $url = "/home/username/public_html/mydomain.com/image/$folder/";
}

$config['upload_path'] = $url;
// etc...