CodeIgniter Forums
Little problem with new file name function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Little problem with new file name function (/thread-60116.html)



Little problem with new file name function - El Forum - 01-19-2014

[eluser]Unknown[/eluser]
Hello everybody, i've got a little problem with my code when I type "http://localhost/ci/index.php/admin/new_file_name/MB.jpg" into my browser. Of course file MB.jpg exist in upload folder.

I've got VARCHAR(20) in database and i want to cut uploaded filename to max. 18 chars and ad number up to 99 to the filename when file existed. For example if myfilename.jpg exists, function will return myfilename1.jpg etc.

So this is my code
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

session_start();
class Admin extends CI_Controller {
    
    public $upload_dir = 'upload';
    
    function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper(array('form', 'url'));
    }
    function index()
    {
        if($this->is_logged())
        {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['user'];
            $this->load->view('panel', $data);
        }
        else
        {
            redirect('login', 'refresh');
        }
    }
    function logout()
    {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('admin', 'refresh');
    }
    
    function is_logged()
    {
        if($this->session->userdata('logged_in'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function upload()
    {
        if($this->is_logged())
        {
            $config['upload_path'] = './' . $this->upload_dir . '/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
        
            $this->load->library('upload', $config);
            
            //$this->load->view('upload');
            
            if (empty($_FILES['userfile']['name']))
            {
                $this->load->view('upload');
            }
            elseif(!$this->upload->do_upload())
     {
  $data = array('inf' => $this->upload->display_errors());
  $this->load->view('upload', $data);
     }
     else
     {
                $s = $this->upload->data();
  $data = array('inf' => 'Plik o nazwie ' . $s['file_name'] . ' zostaƂ wgrany na serwer');
  $this->load->view('upload', $data);
     }
        }
    }
    /*private function _new_file_name($filename)*/
    function new_file_name($filename)
    {
        $f = explode('.', $filename);
        $file = $f[0];
        $ext = end($f);
        $ext = strtolower($ext);

        
        $file = substr($file, 0, 18);
        $url = base_url($this->upload_dir . '/' . $file);
        
        $uri = $url . '.' . $ext;
        $i = 1;
        
        //echo $uri;
        
        if(file_exists($uri))
        {
            echo $url . '.' . $ext;
            
            echo 'AAA';
        }
        else
        {
            echo 'BBB';
            
            while((!file_exists($uri)) and $i <= 99)
            {
                $uri = $url . (string)$i . '.' . $ext;
                $i++;
                
                //echo $uri;
            }
        }
    }
}
?&gt;

The script echos "BBB". What is the reason of this behaviour when the file exists? When script display $uri on the screen I can access the file. Please for help, maybe I've made a little, stupid mistake ;-) Have you got any other ideas to except same file names?


Little problem with new file name function - El Forum - 01-19-2014

[eluser]CroNiX[/eluser]
Because of this:
Code:
$url = base_url($this->upload_dir . '/' . $file);
        
$uri = $url . '.' . $ext;
if(file_exists($uri))

Code:
$uri now = http://yoursite.com/some_dir/the_file.ext

It should be a filepath
Code:
/the/file/path/to/the_file.ext
, not the full url. It's a file on the filesystem, not a web resource.

PHP Manual: file_exists()


Little problem with new file name function - El Forum - 01-19-2014

[eluser]Unknown[/eluser]
Thank you ;-)