简体   繁体   English

Codeigniter 在安装文件夹外的根目录上传文件

[英]Codeigniter upload files at root directory outside of the installation folder

This type of question has been asked here before, but no solution is working in this case.此类问题之前已在此处提出,但在这种情况下没有解决方案。 I have a website admin panel developed in CI and my directory looks like this.我有一个用CI开发的网站管理面板,我的目录看起来像这样。

uploads
    /stores
    /products
admin
    /application
    /system
    /assets

Now, I have to upload files in the subfolders of uploads that are residing in the root via a controller.现在,我必须通过控制器上传位于根目录中的上传子文件夹中的文件。

I have looked around other solutions and I have tried followings:我环顾了其他解决方案,并尝试了以下方法:

$upload_path = './uploads/stores/';

Above code is working when I keep uploads folder inside the admin folder.当我将上传文件夹保存在管理文件夹中时,上面的代码正在工作。 But I need to keep the uploads folder outside the admin.但我需要将上传文件夹保留在管理员之外。

I looked at the path CI uses and based on this, here is another method I tried我查看了CI使用的路径并基于此,这是我尝试的另一种方法

$upload_path = '../../uploads/stores/';

and also,并且,

$upload_path = '/home/domain/public_html/uploads/stores/';

But this gives me following error.但这给了我以下错误。

The path to the image is not correct.图像的路径不正确。

I'm completely lost here.我完全迷失在这里。 Any suggestion is appreciated.任何建议表示赞赏。

You can assign following path when you upload your images.您可以在上传图片时指定以下路径。

    $uploadpath = $_SERVER['DOCUMENT_ROOT'].'/your_folderame';

The document root directly under which the current script is executing, as define in the server configuration file.当前脚本直接在其下执行的文档根目录,如服务器配置文件中所定义。

Use APPPATH and FCPATH to make the correct structure:使用 APPPATH 和 FCPATH 来制作正确的结构:

  1. FCPATH : path to folder containing you project FCPATH :包含您的项目的文件夹的路径
  2. APPPATH : path to application folder APPPATH : 应用程序文件夹的路径

    echo FCPATH .'uploads'.DIRECTORY_SEPARATOR.'stores';
if($this->input->post('img_status') == 3){//here 3 is indicating that new image is selected
    if(!empty($_FILES['image']['name'])){
        $name_array = '';
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png|bmp'; 
        $config['max_size'] = '1000'; 
        $config['max_width'] = '1280'; 
        $config['max_height'] = '1280'; 
        $this->load->library('upload'); 
        $this->upload->initialize($config);
        if(!$this->upload->do_upload('image'))
            $this->upload->display_errors(); 
        else { 
            $fInfo = $this->upload->data(); //uploading
            $this->gallery_path = realpath(APPPATH . '../uploads');//fetching path
            $config1 = array(
                  'source_image' => $fInfo['full_path'], //get original image
                  'new_image' => $this->gallery_path.'/thumb', //save as new image //need to create thumbs first
                  'maintain_ratio' => true,
                  'width' => 250,
                  'height' => 250

                );
            $this->load->library('image_lib', $config1); //load library
            $this->image_lib->resize(); //generating thumb
            $imagename=$fInfo['file_name'];// we will get image name here
            $dataInsert['image'] = $imagename;
        }   
    }
}
else if($this->input->post('img_status') == 2){
    $dataInsert['image'] = NULL;
}

Directory handling is very easy for each directoy you want to move up ../ so对于您要向上移动的每个目录,目录处理非常容易../ 所以

Try this, just keep adding ../ until you get where you want to go.试试这个,继续添加 ../ 直到你到达你想去的地方。 Code: $dir = "../../gallery/safe/";代码:$dir = "../../gallery/safe/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM