如何解决在Codeigniter中上载图像显示错误上载路径似乎无效
||$config[\'upload_path\'] = site_path().\'photos/\';
$config[\'allowed_types\'] = \'gif|jpg|png|jpeg\';
$config[\'max_size\'] = \'2048\';
$this->load->library(\'upload\',$config);
if ( ! $this->upload->do_upload())
{
$this->data[\'alert\'] = $this->upload->display_errors();
$this->load->view(\'profile/photo\',$this->data);
}
else
{
$upload_data = $this->upload->data();
$filename = $upload_data[\'file_name\'];
$width = $upload_data[\'image_width\'];
$height = $upload_data[\'image_height\'];
$config1 = array();
$this->load->library(\'image_lib\');
$config1[\'source_image\'] = site_path().\'photos/\'.$filename;
$this->remove_existing_file($this->session->userdata(\'user_id\'));
$this->Profile_model->savephoto(\'Edit\',$filename );
redirect(\'/profile/photo\');
}
我收到此错误:
上载路径似乎无效。
解决方法
发生此错误的原因只有几个:
目录
site_path().\'photos/\'
不存在,请尝试运行is_dir()
以确保它存在。
该目录存在,但不可写。确保在目录上设置了适当的权限。尝试运行is_writable()
以确保。
您要使用的目录已存在,但尚未正确将其表示在Upload库中。尝试使用带有斜杠的绝对路径,类似于《用户指南》中的示例。
除此之外,我没有想到的任何解释。这是用于验证路径的CI代码(属于Upload类的一部分):
public function validate_upload_path()
{
if ($this->upload_path == \'\')
{
$this->set_error(\'upload_no_filepath\');
return FALSE;
}
if (function_exists(\'realpath\') AND @realpath($this->upload_path) !== FALSE)
{
$this->upload_path = str_replace(\"\\\\\",\"/\",realpath($this->upload_path));
}
// This is most likely the trigger for your error
if ( ! @is_dir($this->upload_path))
{
$this->set_error(\'upload_no_filepath\');
return FALSE;
}
if ( ! is_really_writable($this->upload_path))
{
$this->set_error(\'upload_not_writable\');
return FALSE;
}
$this->upload_path = preg_replace(\"/(.+?)\\/*$/\",\"\\\\1/\",$this->upload_path);
return TRUE;
}
更新:
根据您的评论,尝试使用此方法,让我们看看在进行下一步调试之前会发生什么:
$config[\'upload_path\'] = \'./community/photos/\';
, $config[\'upload_path\'] = \'./photos/\';
$config[\'allowed_types\'] = \'gif|jpg|jpeg|png\';
$config[\'max_size\'] = \'1000\';
$config[\'max_width\'] = \'1920\';
$config[\'max_height\'] = \'1280\';
$this->upload->initialize($config);
将此代码写在您的控制器块中
这个问题很常见,我知道您在__construct()块中编写了此代码,但是正确的方法是在调用上载代码的特定Controller中使用此代码。
, 这应该有帮助
如果您需要在上传文件时自动创建以下目录:./assets/2016-07-27/
,则必须扩展Upload库以自动创建目录(如果目录不存在)。
代码:
<?php
defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');
/**
* File Uploading Class Extension
*
* @package CodeIgniter
* @subpackage Libraries
* @category Uploads
* @author Harrison Emmanuel (Eharry.me)
* @link https://www.eharry.me/blog/post/my-codeigniter-upload-extension/
*/
class MY_Upload extends CI_Upload {
/**
* Validate Upload Path
*
* Verifies that it is a valid upload path with proper permissions.
*
* @return bool
*/
public function validate_upload_path()
{
if ($this->upload_path === \'\')
{
$this->set_error(\'upload_no_filepath\',\'error\');
return FALSE;
}
if (realpath($this->upload_path) !== FALSE)
{
$this->upload_path = str_replace(\'\\\\\',\'/\',realpath($this->upload_path));
}
if ( ! is_dir($this->upload_path))
{
// EDIT: make directory and try again
if ( ! mkdir ($this->upload_path,0777,TRUE))
{
$this->set_error(\'upload_no_filepath\',\'error\');
return FALSE;
}
}
if ( ! is_really_writable($this->upload_path))
{
// EDIT: change directory mode
if ( ! chmod($this->upload_path,0777))
{
$this->set_error(\'upload_not_writable\',\'error\');
return FALSE;
}
}
$this->upload_path = preg_replace(\'/(.+?)\\/*$/\',\'\\\\1/\',$this->upload_path);
return TRUE;
}
}
如何使用:
只需创建application/libraries/MY_Upload.php
并将上面的代码粘贴到其中即可。
就这样!
更多信息:
GitHub Gist。
我在Eharry.me上的博客文章
注意:
此扩展与CodeIgniter 3.x和2.x版本兼容。
, 如果您在Windows上,请尝试写入\“ c:\\\”,然后查看是否可行。
还有site_path()的输出是什么,例如echo site_path();。 ?
在xammp上,我发现我需要写c:\\ xammp \\ htdocs \\ myproject \\ photos \\而不是仅使用\'\\ photos \\\';
, 您好在服务器上,您可以连接filezilla并右键单击文件夹并更改文件属性
, 检查您的.htaccess文件不阻止应用程序文件夹
检查上传的是CHMOD 777
使用var_dump(is_dir(\'/ photos / \'));看看您的目录是否存在!
最后尝试一下:
$config[\'upload_path\'] = \'photos/\';
$config[\'allowed_types\'] = \'gif|jpg|jpeg|png\';
$config[\'max_size\'] = \'1000\';
$config[\'max_width\'] = \'1920\';
$config[\'max_height\'] = \'1280\';
$this->upload->initialize($config);
, 我认为问题可以通过使用解决
$config[\'upload_path\'] =\'./photos/\';
代替
$config[\'upload_path\'] = site_path().\'photos/\';
, 我知道这已经得到回答。即使是我也很难尝试上传文件。但就我而言,问题是我在/ var / www以外的文件夹中有CodeIgniter应用程序,而www文件夹中有索引页(主要是出于安全措施)。
uploads文件夹应位于包含index.php文件的/ var / www文件夹中。这就是我要做的。接下来是将777权限授予上载文件夹。
, 我在Windows上使用湿巾
$config[\'upload_path\'] = base_url().\'./resources/uploads/\';
根本不工作。
var_dump(is_dir($config[\'upload_path\'])); //return false
var_dump(is_writable($config[\'upload_path\'])); //return false
但是当我将其更改为
$config[\'upload_path\'] = \'./resources/uploads/\'; //works fine
它工作正常。所以我想在Windows上这不是权限问题,这是由base_url()方法引起的,这有多奇怪。
, 在您的控制器中试用此代码
$config[\'upload_path\'] = \'images\'; //name of the uploading folder
$config[\'allowed_types\'] = \'jpeg|png|jpg\';
$config[\'file_name\'] = \'name_for_file\';
$this->load->library(\'upload\',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload())
{
echo $this->upload->display_errors();
exit;
}
else
{
$upload_data = $this->upload->data();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。