Welcome Guest, Not a member yet? Register   Sign In
Upload class, doesn't work with simple quote in file name [solved]
#1

[eluser]jeanv[/eluser]
Hi everyone,

i have a small problem. I made an upload file with CI, and next the uploaded file is send by email, but when the filename contains a simple quote it's not working, here is the code:
Code:
if(!empty($_FILES['userfile']['name']))
            {
                // Upload
                $config['upload_path']         = './upload/';
                $config['allowed_types']    = 'gif|jpg|jpeg|png|txt|doc|docx|xls|xlsx|tiff|ppt|pptx|pdf|zip|rar';
                //$config['max_size']            = '1024'; // 1Mo
                $config['remove_spaces']    = 'true';
                $config['overwrite']        = 'true';
                
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => '<div id="errors">'.$this->upload->display_errors().'</div>');
                    $this->load->view('form', $error);
                    
                }else
                {
                    $data = array('upload_data' => $this->upload->data());

                    $doc_uploaded = str_replace(' ', '_', $_FILES['userfile']['name']);
                    $to_attach = "upload/".stripslashes($doc_uploaded);
                    $this->email->attach($to_attach);
                }
            }
            
            // email
            if ( (!empty($_FILES['userfile']['name']) && $this->upload->do_upload()) || empty($_FILES['userfile']['name']))
                $this->email->send();

it's working great with files that do not contain quote... i mean "'"...
what can i do ?

thanks a lot !
bye
#2

[eluser]pistolPete[/eluser]
[quote author="jeanv" date="1237482707"]...it’s not working...[/quote]

We need a better error description.
Do you get any error message? Is your email sent? etc.
#3

[eluser]TheFuzzy0ne[/eluser]
Have you tried just printing the contents of the file you need to send? I'm wondering if you need to run the filename through something like this first:
Code:
$filename = strtr($filename, array(
        "'" => "\\'",
        '"' => '\"',
        " " => "_"
    ));

That'll replace spaces with underscores and escape single and double quotes.

EDIT: Also:
Code:
$config['remove_spaces']    = 'true';
$config['overwrite']        = 'true';

You should use TRUE here, and not 'true'. Then you shouldn't have to replace spaces with underscores manually.

You can also echo $this->email->print_debugger() after the message has been sent, to see if the problem is not anything obvious.
#4

[eluser]jeanv[/eluser]
the mail is sent but does not contain anything. The upload is done correctly, the name of the uploaded file is re-written correctly (for example spaces are replaced by "_").

i don't know how to know the filename to modify it.
#5

[eluser]jeanv[/eluser]
and yes i have these two lines:

$config['remove_spaces'] = 'true';
$config['overwrite'] = 'true';
#6

[eluser]jeanv[/eluser]
ok i solved using the code
Code:
$filename = strtr($filename, array(
        "'" => "\\'",
        '"' => '\"',
        " " => "_"
    ));

thanks a lot !

bye




Theme © iAndrew 2016 - Forum software by © MyBB