Uploading an image in Codeigniter shows error The upload path does not appear to be valid

前端 未结 10 1583
独厮守ぢ
独厮守ぢ 2020-12-18 11:54
$config[\'upload_path\'] = site_path().\'photos/\';
$config[\'allowed_types\'] = \'gif|jpg|png|jpeg\';
$config[\'max_size\'] = \'2048\';
$this->load->library(\         


        
相关标签:
10条回答
  • 2020-12-18 12:22
    $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);
    

    write this code in your controller block

    this problem is common i know your wrote this code in __construct() block but right method is use this code in particular Controller where upload code are call.

    0 讨论(0)
  • 2020-12-18 12:23

    Check your .htaccess file not to block application folder Check is uploads is CHMOD 777 Use var_dump(is_dir('/photos/')); to see is your directory exist! Finally try this:

        $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);
    
    0 讨论(0)
  • 2020-12-18 12:26

    This should help

    If you need to automatically create directories like: ./assets/2016-07-27/ when uploading files then you'll have to extend the Upload library to automatically create directories if they don't exist.

    The code:

    <?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;
        }
    }
    


    How to use:

    Simply create application/libraries/MY_Upload.php and paste the code above in it. That's all!


    More info:

    • The GitHub Gist.
    • My blog post on Eharry.me


    NOTE:

    This extension is compatible with CodeIgniter 3.x and 2.x versions.

    0 讨论(0)
  • 2020-12-18 12:29

    Try out this code in your Controller

    $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();
     } 
    
    0 讨论(0)
  • 2020-12-18 12:33

    If you are on windows try writing to "c:\" and see if that works.

    Also what is the output of site_path() eg echo site_path(); ?

    On xammp I've found I need to write c:\xammp\htdocs\myproject\photos\ instead of just using '\photos\';

    0 讨论(0)
  • 2020-12-18 12:33

    I'm using wamp on windows

    $config['upload_path'] = base_url().'./resources/uploads/';
    

    not working at all.

    var_dump(is_dir($config['upload_path'])); //return false
    var_dump(is_writable($config['upload_path']));  //return false
    

    but when I change it to

    $config['upload_path'] = './resources/uploads/';   //works fine
    

    it works fine. So I guess on windows it's not a permission problem.it's caused by the base_url() method .how strange is that.

    0 讨论(0)
提交回复
热议问题