4

现在我已经尝试了我读过的大部分修复,其中大多数提到了 APPPATH、base_url()、真实路径等,但我真的不知道为什么它们都不起作用,什么起作用我是我使用了实际路径,而不是 url,而是带有 C:\xampp\htdocs 的路径。等等等等等等。现在我已经阅读了一个线程,即 url 和目录不是一回事。 .并且upload_path只接受目录路径,我的意思是服务器上上传文件夹的实际位置而不是URL ..现在我的问题是APPPATH为什么不起作用。据我所知,它是实际的目录路径。但是当我试图显示它是否只返回“../applicaiton/”时,在上传的 $config['upload_path'] 上使用的最佳路径是什么。

编辑:

我在一个单独的文件中有这个... upload.php

<?php 
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '1600';
$config['max_height'] = '1200';

这是我的控制器

     if($this->upload->do_upload('image'))
     {
         //Did something here
     }
     else
     {
        //Did something for errors like display_errors()
     }

最终结果是它显示“上传路径似乎无效”,我也尝试过这些代码行

试验一:

$config['upload_path'] ='./uploads/';

试验二:

$config['upload_path'] ='uploads/';

试验 3:

$config['upload_path'] ='/admin/assets/uploads/';

试验四:

$config['upload_path'] ='./admin/assets/uploads/';

试验 5:

$config['upload_path'] ='admin/assets/uploads/';

唯一有效的是这个

 $config['upload_path'] ='C:\xampp\htdocs\practice\admin.abcgencon\admin\assets\uploads'';

并且使用最后一部分作为路径有点混乱,所以我也尝试过APPPATH,但它也不起作用,它也显示“../application”..

正如@cryptic 所说,我已经发布了这个代码片段。

4

8 回答 8

18

realpath问题,你试过APPPATH分开吗?

在 CodeigniterAPPPATH中指向应用程序文件夹。

示例:(将您的文件夹放在应用程序文件夹之外,只是说如果您没有这样做)假设我们要放置名为images的文件的文件夹。

所以你需要做的是结合realpath() 和APPPATH

$image_path = realpath(APPPATH . '../images');

并将其传递给您的配置

$config['upload_path'] = $image_path;
于 2013-02-24T11:47:03.330 回答
1

创建您的文件上传目录,例如在应用程序目录之外或 CI 的根目录中上传,并设置上传路径如下:-

$config['upload_path'] = realpath(FCPATH.'uploads');

FCPATH:index.php 所在的前端控制器的路径(CI 的根目录)

上面的代码在服务器和本地都运行。

于 2017-04-12T10:29:33.277 回答
0

这就是在 CI 中完成文件上传的方式

$cat_image_name = $_FILES["cat_image"]["name"] ; 

//file uploading params
$config['upload_path'] = './uploaded_files/categories';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $image_id."_ipad";
$config['remove_spaces'] = TRUE;

//Loading Library - File Uploading
$this->load->library('upload', $config);

//Upload the image(Ipad)
if (!empty($cat_image_name)) 
{
  $this->upload->do_upload('cat_image');
  $data = array('upload_data' => $this->upload->data());
  $category_image_ipad = $data['upload_data']['file_name'];
  $img_extension = $data['upload_data']['file_ext'];
}
于 2013-02-24T11:23:14.207 回答
0

文件上传需要多部分表单。为此,您必须包含一个表单助手。

转到配置文件夹,单击自动加载并找到$autoload['helper'] = array();并放置“表单”:

$autoload['helper'] = array('form');

对于控制器:

$config = 数组(

    'upload_path' => "./assets/",
    'allowed_types' => "gif|jpg|png|jpeg",
    'overwrite' => TRUE,
    'max_size' => "2048000",
    'max_height' => "768",
    'max_width' => "1024"
    );

    $this->load->library('upload', $config);

    if(!$this->upload->do_upload()){
        $errors = array('error' => $this->upload->display_errors());
        $post_image = 'noimage.jpg';
    } else {
        $data = array('upload_data' => $this->upload->data());
        $post_image = $_FILES['userfile']['name'];
    }

    $this->user_model->create_photo($post_image);
    redirect('');
}

型号:

public function create_photo($post_image){

    $data = array(
        'pri' => $this->input->post('price'),
        'descrip' => $this->input->post('title'),
        'post_image' => $post_image
    );

    return $this->db->insert('table_name', $data);
}
于 2018-05-17T07:49:22.870 回答
0

尝试这个

$config['upload_path'] = '../uploads/;

这对我来说效果很好

于 2016-10-19T06:52:14.393 回答
0

在 Windows 和 Linux 上测试:

$path = realpath(".");
$path .= "/uploads/profile_images";

这里的 uploads 是根目录下的上传目录。

于 2018-05-30T15:19:36.360 回答
0

用它:

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
于 2017-07-24T08:24:10.397 回答
-1

这很简单:

只需将所有 $config 复制到名为 upload.php 的新 php 文件中,然后将其放入 cofig 文件夹中。

你的路径会正常工作。

这是upload.php文件内容:

<?php

    $config['upload_path'] = site_url().'uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
    $config['max_size'] = 0;
    $config['max_width']  = 0;
    $config['max_height']  = 0;
    $config['file_ext_tolower']  = TRUE;
    $config['overwrite']  = FALSE;
    $config['max_filename']  = 0;
    $config['encrypt_name']  = TRUE;
    $config['remove_spaces']  = TRUE;
    $config['detect_mime']  = TRUE;
    $config['mod_mime_fix']  = TRUE;
?>

但也请在upload_file 控制器中保留$config 数据。例如:

public function do_upload(){

    $config['upload_path'] = 'uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
    $config['max_size'] = 0;
    $config['max_width']  = 0;
    $config['max_height']  = 0;
    $config['file_ext_tolower']  = TRUE;
    $config['overwrite']  = FALSE;
    $config['max_filename']  = 0;
    $config['encrypt_name']  = TRUE;
    $config['remove_spaces']  = TRUE;
    $config['detect_mime']  = TRUE;
    $config['mod_mime_fix']  = TRUE;

//Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:

   $this->upload->initialize($config);


            if (!$this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
    }

很简单...

于 2015-12-29T06:20:32.320 回答