在Codeigniter中上载图像显示错误上载路径似乎无效

时间:2011-05-28 05:27:43

标签: 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');
}

我收到此错误:

  

上传路径似乎无效。

10 个答案:

答案 0 :(得分:8)

发生此错误的原因只有几个:

  1. 目录site_path().'photos/'不存在,请尝试运行is_dir()以确保其存在。
  2. 目录存在但不可写。确保已在目录上设置适当的权限。尝试运行is_writable()以确保。
  3. 您要使用的目录存在,但您尚未将其正确表示到上载库。尝试使用带有正斜杠的绝对路径,类似于the example in the User Guide
  4. 除此之外,没有我能想到的解释。以下是验证路径的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/';
    

答案 1 :(得分:3)

$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()块中编写了这段代码但是正确的方法是使用这段代码特别是控制器上传代码被调用。

答案 2 :(得分:1)

这应该有帮助

如果您在上传文件时需要自动创建:./assets/2016-07-27/这样的目录,那么您必须扩展上传库以自动创建目录(如果它们不存在)。

代码:

<?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并将上面的代码粘贴到其中。 这就是全部!


更多信息:


注意:

此扩展程序与CodeIgniter 3.x和2.x版本兼容。

答案 3 :(得分:0)

如果您在Windows上,请尝试写入“c:\”,看看是否有效。

还有什么是site_path()的输出,例如echo site_path(); ?

在xammp上我发现我需要编写c:\ xammp \ htdocs \ myproject \ photos \而不是只使用'\ photos \';

答案 4 :(得分:0)

您好,在服务器上,您可以连接filezilla并右键单击文件夹并更改文件属性

答案 5 :(得分:0)

检查.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);

答案 6 :(得分:0)

我认为问题可以通过使用

来解决
$config['upload_path'] ='./photos/';

而不是

$config['upload_path'] = site_path().'photos/';

答案 7 :(得分:0)

我知道这已经得到了解答。即使我尝试上传文件也很困难。但在我的情况下,问题是我在/ var / www之外的文件夹中有CodeIgniter应用程序,在www文件夹中有索引页面(主要是作为安全措施)。

uploads文件夹应位于包含index.php文件的/ var / www文件夹中。这就是我必须要做的。接下来就是为uploads文件夹提供777权限。

答案 8 :(得分:0)

我在Windows上使用wamp

$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()方法引起的。这就是奇怪的。

答案 9 :(得分:0)

控制器

中试用此代码
List<Foo> fooList = Arrays.<Foo>asList(new FooBar());
//                         ^^^^^ -- set generic types before method name