Welcome Guest, Not a member yet? Register   Sign In
rename uploaded file - bug?
#1

[eluser]Ickes[/eluser]
Hello.

Could very well be me but I was having a problem renaming a file when using the upload library. I have come up with a solution too (albeit not that pretty of one).

Currently Upload.php library reads as...
Code:
// Set the uploaded data as class variables
        $this->file_temp = $_FILES[$field]['tmp_name'];        
        $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
        $this->file_size = $_FILES[$field]['size'];        
        $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
        $this->file_type = strtolower($this->file_type);
        $this->file_ext     = $this->get_extension($_FILES[$field]['name']);

The issue comes in when you set $config['file_name'] before running $this->upload->do_upload();

I could not get my $config['file_name'] to be the name of the new file. It seems the upload library was overwriting
Code:
$this->file_name
//as given in the $config['file_name']
with
Code:
$this->_prep_filename($_FILES[$field]['name'])

Does that make sense? Here is the fix I came up with...
Code:
// Set the uploaded data as class variables
        $this->file_temp = $_FILES[$field]['tmp_name'];        
//      $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
//the above line is the issue - check below for the replacement

        $this->file_size = $_FILES[$field]['size'];        
        $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
        $this->file_type = strtolower($this->file_type);
        $this->file_ext     = $this->get_extension($_FILES[$field]['name']);


//---EDIT - bug? wouldn't write new filename---------------------------------------------
if(empty($this->file_name))
{
     //$config did NOT set file_name so continue as normal...
     $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
}
else
{
     //$config did SET file_name so use file_name with the extenstion
     $this->file_name = $this->file_name.strtolower($this->file_ext);

     //this->file_name needs cleansing to be sure it is not malicious
}
//---end EDIT----------------------------------------------------------------------------

Thoughts?
#2

[eluser]BrianDHall[/eluser]
I haven't seen this problem when using the upload class, so perhaps you could post code that you find exhibits this bug in naming?

My experience was that if I set $config variables then loaded them such as:

Code:
$config['file_name'] = 'newfile';
$config['overwrite'] = false;

$this->load->library('upload', $config);

// or the second upload

$this->upload->initialize($config);

...then it will work just fine, though I am unsure of exactly what happens if you try to rename to a file name that already exists. For this reason I preferred using encrypt_name=true and overwrite=false options.
#3

[eluser]dunken[/eluser]
I have the same problem as Ickes.

controller
Code:
$config['upload_path'] = 'files/';
        $config['allowed_types'] = 'doc|xls|txt|pdf|gif|jpg|png';
        $config['file_name'] = time();
        
        if($this->document_model->add_document($flowHeader, $flowName, $flow_section, $config))
model

Code:
function add_document($header, $flow, $section, $config){
        //loadar uploaden
        
        $this->load->library('upload', $config);
        if($this->upload->do_upload()){
            $docData = $this->upload->data();

//.......

1. First of all I must say that the mime-type-problem is pretty annoying, u must have the pictures at the end.

2. While i know that it reads the $config (it puts my files on the right place) it dosent seam to care about my $config[file_name] = time();

Is it only the two of us that has this problem?
#4

[eluser]Unknown[/eluser]
this bug is fixed at last version
http://dev.ellislab.com/svn/CodeIgniter/...Upload.php
#5

[eluser]Ian Jones[/eluser]
I'm glad I found this thread, thought I was going bonkers.

So this is fixed in versions later than the current? > 1.7.2
#6

[eluser]onlyjf[/eluser]
The better way is http://codeigniter.com/bug_tracker/bug/10800/




Theme © iAndrew 2016 - Forum software by © MyBB