【问题标题】:Uploading an image in Codeigniter shows error The upload path does not appear to be valid在 Codeigniter 中上传图像显示错误上传路径似乎无效
【发布时间】:2011-05-28 05:27:42
【问题描述】:
$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');
}

我收到此错误:

上传路径似乎无效。

【问题讨论】:

    标签: codeigniter


    【解决方案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 版本兼容。

    【讨论】:

      【解决方案2】:

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

      1. site_path().'photos/' 目录不存在,尝试运行is_dir() 以确保它存在。
      2. 目录存在但不可写。确保您已对目录设置适当的权限。尝试运行is_writable() 以确保。
      3. 您要使用的目录已存在,但您尚未将其正确呈现给上传库。尝试使用带有尾部正斜杠的绝对路径,类似于the example in the User Guide

      除此之外,我想不出任何解释。下面是验证路径的 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;
      }
      

      更新:

      根据你的 cmets,试试这个,让我们看看在进入下一步调试之前会发生什么:

      $config['upload_path'] = './community/photos/';
      

      【讨论】:

      • var_dump(is_dir(site_path().'photos/')) 返回什么?
      • 那么这就是您在 Codeigniter 中收到错误的原因。 site_path().'photos/' 不是目录。您的路径一定有错误,或者没有创建它。改用./community/photos/ 作为路径。
      • 是的,当我给出 .photos/.but 时出现 2 个错误 遇到 PHP 错误 严重性:8192 消息:不推荐使用函数 eregi() 文件名:libraries/Image_lib.php 行号:353遇到 PHP 错误 严重性:警告消息:无法修改标头信息 - 标头已由(输出开始于 F:\xampp\htdocs\community\system\libraries\Exceptions.php:166)文件名:helpers/url_helper.php 行编号:622
      • 我建议你go to the Codeigniter website 并获取最新版本。版本 1.X 不再受支持,看起来您使用的是 PHP 5.3。当前版本是 2.0.2,请务必查看 upgrade instructionschange log,您需要进行一些非常简单的更改。相信我——花一点时间是值得的。
      【解决方案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() 块中编写了此代码,但正确的方法是在调用上传代码的特定控制器中使用此代码。

      【讨论】:

        【解决方案4】:

        在您的控制器

        中试用此代码
        $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();
         } 
        

        【讨论】:

          【解决方案5】:

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

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

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

                我认为问题可以通过使用来解决

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

                而不是

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

                【讨论】:

                  【解决方案9】:

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

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

                  【讨论】:

                    【解决方案10】:

                    我在 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() 方法引起的。这有多奇怪。

                    【讨论】:

                      猜你喜欢
                      • 2020-12-31
                      • 1970-01-01
                      • 2011-12-28
                      • 1970-01-01
                      • 2018-09-02
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-10-13
                      相关资源
                      最近更新 更多