Codeigniter上的上载路径返回“上载路径似乎无效”

eni9jsuy  于 2022-12-07  发布在  其他
关注(0)|答案(9)|浏览(146)

Now i've tried most of the fixes that i've read, most of them mention about APPPATH, base_url(), real path and etc. but i really don't know why all of them didn't work, what worked for me is that i've used the actual path, not a url but the one with the C:\xampp\htdocs.. blah blah blah.. now i've read one thread that url and directory aren't the same thing.. and the upload_path accepts only directory path and i mean the actual location of the uploads folder on the server not the URL.. now my question is how come APPPATH don't work. as what i know it the actual directory path. but when i tried to display is it return only "../applicaiton/" what really is the best path to be used on the $config['upload_path'] on the upload.php specially when deploying it to an actual server it is really a nuisance finding the directory path of your upload folder, NOTE im not using the initialize() method i'm putting my configs on config/upload.php
EDITS:
i have this on a separate file... 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';

and this is my controller

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

and end result is it displays "the upload path doesn't seem to be valid" and i've tried these line of code also
Trial 1:

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

Trial 2:

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

Trial 3:

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

Trial 4:

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

Trial 5:

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

the only thing that works is this

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

and using the last part as a path is kinda messy so i've tried also APPPATH but it doesn't work also it also display "../application"..
as @cryptic said i've posted this code snippet.

pxiryf3j

pxiryf3j1#

问题,您分别尝试了realpathAPPPATH
在Codeigniter中APPPATH指向应用程序文件夹。

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

因此,您需要将realpath()和APPPATH组合起来

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

并将其传递给配置

$config['upload_path'] = $image_path;
wsewodh2

wsewodh22#

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

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

FCPATH:index.php所在的前端控制器的路径(CI的根目录)
上面的代码在服务器和本地都运行。

vulvrdjw

vulvrdjw3#

这是在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'];
}
kninwzqo

kninwzqo4#

试试这个

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

对我来说效果很好

zpgglvta

zpgglvta5#

使用它:

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
pdsfdshx

pdsfdshx6#

文件上载需要多部分表单。为此,您必须包含表单帮助器。
后藤config文件夹,单击autoload并找到$autoload['helper'] = array();,然后输入“form”:

$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);
}
pbossiut

pbossiut7#

在Windows和Linux上测试:

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

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

ghhkc1vu

ghhkc1vu8#

FCPATH:index.php所在的前端控制器路径或文件夹
APPPATH:****应用程序文件夹

root文件夹下创建uploads文件夹:

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

application文件夹下创建uploads文件夹:

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

如果:如果您在root文件夹之外创建了uploads文件夹:

$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads');
l3zydbqr

l3zydbqr9#

事情很简单:
只需将所有的$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);
    }
    }

很简单...

相关问题