php Codeigniter 上的上传路径返回“上传路径似乎无效”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15050322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload path on Codeigniter returns "the upload path doesn't seem to be valid"
提问by lemoncodes
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
现在我已经尝试了我读过的大部分修复程序,其中大部分都提到了 APPPATH、base_url()、真实路径等,但我真的不知道为什么它们都不起作用,什么起作用了我是说我使用了实际路径,而不是 url,而是使用 C:\xampp\htdocs 的路径。等等等等。 . 并且 upload_path 只接受目录路径,我的意思是服务器上上传文件夹的实际位置而不是 URL .. 现在我的问题是 APPPATH 怎么不起作用。据我所知,它是实际的目录路径。但是当我试图显示它是否只返回“../applicaiton/”时,上传时在 $config['upload_path'] 上使用的最佳路径是什么。
EDITS:
编辑:
i have this on a separate file... upload.php
我有这个在一个单独的文件...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:
试验 1:
$config['upload_path'] ='./uploads/';
Trial 2:
试验 2:
$config['upload_path'] ='uploads/';
Trial 3:
试验 3:
$config['upload_path'] ='/admin/assets/uploads/';
Trial 4:
试验 4:
$config['upload_path'] ='./admin/assets/uploads/';
Trial 5:
试验 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 APPPATHbut it doesn't work also it also display "../application"..
并使用最后一部分作为路径有点混乱,所以我也尝试过,APPPATH但它也不起作用,它还显示“../application”..
as @cryptic said i've posted this code snippet.
正如@cryptic 所说,我已经发布了这个代码片段。
回答by
Question, you tried realpathand APPPATHseperately?
问,你试过realpath和APPPATHseperately?
In Codeigniter APPPATHpoints to the application folder
在 Codeigniter 中APPPATH指向应用程序文件夹
Example: (place your folder outside the application folder, just saying if you did not do that way) lets say te folder where we want to place the files called images
示例:(将您的文件夹放在应用程序文件夹之外,只是说如果您没有这样做)让我们说 te 文件夹,我们要在其中放置名为图像的文件
So what you need to do is combine realpath() and APPPATH
所以你需要做的是结合realpath() 和APPPATH
$image_path = realpath(APPPATH . '../images');
and pass it to your config
并将其传递给您的配置
$config['upload_path'] = $image_path;
回答by aalov
Create your file upload directory say uploads outside of application directory or at the root directory of CI, and set the upload path as follows:-
创建您的文件上传目录,例如在应用程序目录之外或 CI 的根目录上传,并设置上传路径如下:-
$config['upload_path'] = realpath(FCPATH.'uploads');
FCPATH: Path to the front controller where index.php exists (root of CI)
FCPATH:index.php 所在的前端控制器的路径(CI 的根目录)
The above code runs on both server and local.
上面的代码在服务器和本地运行。
回答by ShirAzi
use it:
用它:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
回答by dark
file upload requires a multipart form. For this, you must have included a form helper.
文件上传需要一个多部分的形式。为此,您必须包含一个表单助手。
goto config folder, click autoload and find the $autoload['helper'] = array();and put 'form' :
转到配置文件夹,单击自动加载并找到$autoload['helper'] = array();并放置“表单”:
$autoload['helper'] = array('form');
for controller:
对于控制器:
$config = array(
$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('');
}
for model:
型号:
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);
}
回答by Noman Ibrahim
Tested on Windows & Linux:
在 Windows 和 Linux 上测试:
$path = realpath(".");
$path .= "/uploads/profile_images";
Here uploads is the uploads directory on root.
这里的uploads 是根目录下的uploads 目录。
回答by Shaolin
This is how file uploading is done in CI
这就是 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'];
}
回答by Ganesh Karthikeyan
try this
尝试这个
$config['upload_path'] = '../uploads/;
It's working very well for me
它对我很有效
回答by Ahasn Khan
It is very simple:
这很简单:
Just copy your all $config in new php file called upload.php and put it in cofig folder.
只需将所有 $config 复制到名为 upload.php 的新 php 文件中,并将其放入 cofig 文件夹中。
You path will work ok.
你的路径会正常工作。
Here is upload.php file contents:
这是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;
?>
But please keep $config data in upload_file controller as well.For example:
但请在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);
}
}
It is simple...
很简单...

