【问题标题】:Codeigniter upload files at root directory outside of the installation folderCodeigniter 在安装文件夹之外的根目录上传文件
【发布时间】:2016-11-23 18:04:56
【问题描述】:

以前曾在此处提出过此类问题,但在这种情况下没有解决方案。我有一个在CI 开发的网站管理面板,我的目录看起来像这样。

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

现在,我必须通过控制器上传位于根目录中的上传子文件夹中的文件。

我查看了其他解决方案,并尝试了以下解决方案:

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

当我将上传文件夹保存在管理文件夹中时,上述代码正在工作。但我需要将上传文件夹保留在管理员之外。

我查看了CI 使用的路径,并基于此,这是我尝试的另一种方法

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

还有,

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

但这给了我以下错误。

图片路径不正确。

我完全迷路了。任何建议表示赞赏。

【问题讨论】:

  • 你试过 $config['upload_path'] = './uploads/stores/' 吗?如果它不起作用,则使用 file_exist 函数检查它是否在同一位置。
  • 我已经试过了,但是没有用
  • print_r($_FILES);正在返回文件的所有数据?你能用代码更新你的问题吗,因为 do_upload() 时可能会出现问题。
  • 是的,我正在获取文件,但问题出在上传路径上。
  • 也是您在 print_r($_FILES) 中返回的文件的数组。

标签: php codeigniter file-upload


【解决方案1】:

您可以在上传图片时指定以下路径。

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

当前脚本直接在其下执行的文档根目录,如服务器配置文件中所定义。

【讨论】:

  • 版本号:$uploadpath = $_SERVER['DOCUMENT_ROOT'].'/folderame';
  • 欲了解更多详情,请访问此用户已直接回答Link
  • @ankitsuthar 此链接已从该页面获取问题和答案。
【解决方案2】:

使用APPPATH和FCPATH做出正确的结构:

  1. FCPATH : 包含您的项目的文件夹路径
  2. APPPATH : 应用程序文件夹路径

    echo  FCPATH .'uploads'.DIRECTORY_SEPARATOR.'stores';
    

【讨论】:

  • 这段代码返回的是我之前尝试过的相同路径,这给了我错误。
  • /home/domain/public_html/uploads/stores
【解决方案3】:
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;
}

【讨论】:

  • 只有代码,我不知道这篇文章如何以及是否回答了这个问题。请参阅how to write a good answer 并进行相应编辑。
【解决方案4】:

对于您要向上移动的每个目录来说,目录处理非常容易../ 所以

试试这个,继续添加 ../ 直到你到达你想去的地方。 代码: $dir = "../../gallery/safe/";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多