Welcome Guest, Not a member yet? Register   Sign In
[Upload Class] Problem using custom file extension
#11

[eluser]BrianDHall[/eluser]
OK, here you go. Fully working code!

Change upload path to your folder, make sure permissions are correct (755 I think works) and ensure it is permitted for access in your .htaccess file.

Function for your controller
Code:
function upload_file()
    {
        $config = array(
        'upload_path' => './uploads',
        'allowed_types' => 'uvc',
        'overwrite' => false,
        'encrypt_name' => true,
        'remove_spaces' => true,
        'max_size' => 500);

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

        if ($this->input->post('upload'))
        {
            $result = $this->upload->do_upload();

            $data = $this->upload->data();

            if ($result)
            {
                echo "It worked!\n";
                var_dump($data);
            }
        }

        $this->content = $this->load->view('fileuploadform');

    }

View:

Code:
<h3>Upload an Image</h3>

&lt;?=form_open_multipart('upload_file')?&gt;
&lt;?=$this->upload->display_errors()?&gt;
&lt;?=form_upload('userfile')?&gt;
&lt;?=form_submit('upload', 'Upload')?&gt;
&lt;?=form_close()?&gt;

Open mimes.php in config folder, should have something like:

Code:
$mimes = array(    'hqx'    =>    'application/mac-binhex40',
                'cpt'    =>    'application/mac-compactpro',
                'uvc'    =>    'text/plain',
...

If you still get file unallowed backup your mimes file and replace whole file with:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| MIME TYPES
| -------------------------------------------------------------------
| This file contains an array of mime types.  It is used by the
| Upload class to help identify allowed file types.
|
*/

$mimes = array('uvc' => 'text/plain');


/* End of file mimes.php */
/* Location: ./system/application/config/mimes.php */

This works great on my system, so if not working be sure to put this in a fresh controller, perhaps on a fresh version of CI - because this should work just fine!

Let me know if you get it working!
#12

[eluser]metalking[/eluser]
Hi !

BrianDHall : I've tested your code, which was quite the same as my code, and found the same error when trying to upload a file. I've tested it with the reduced mimes.php file also, but no difference.
Then I applied the fix described here, and the error message changed.
Now the script can't find the destination directory. But it worked with the .txt extension and without the fix, so my configuration is correct...

??
Thanks
#13

[eluser]BrianDHall[/eluser]
Wow, I'm stumped. It seems it must be your installation somehow. I would recommend creating a temporary fresh install of CodeIgniter 1.7.2 (the new version) and try this uploading on that fresh install with just your upload code and updated mimes.php and see what happens. Nothing is wrong with the code, so that just can't be the cause of the problem.
#14

[eluser]Aken[/eluser]
The only other thing I can think of is your browser is changing the mime type of the file you've uploaded because it doesn't know exactly what it is.
#15

[eluser]metalking[/eluser]
This can't be, because I tested the mime type with the get_mime_by_extension() function and it returned 'text/plain' as expected..
Maybe I'll try it on a fresh install as suggested by BrianDHall.

See you soon




Theme © iAndrew 2016 - Forum software by © MyBB