Welcome Guest, Not a member yet? Register   Sign In
Problems with CI file upload controller and Flex (FileReference class)
#1

[eluser]log2e[/eluser]
I'm trying to use CI's file uploading class for uploading files from a Flex application. So far I've been using my own PHP script for managing file uploads, but now I'd like to switch to a CI-only PHP solution on the server-side. The HTML-based example from the CI User Guide works fine but I can't figure out what I'm doing wrong when it comes to Flex and CI.

According to Adobe's documentation (FileReference class) the server script that handles the file upload should expect an HTTP POST request with the following elements:

- Content-Type with a value of multipart/form-data.
- Content-Disposition with a name attribute set to "Filedata" and a filename attribute set to the name of the original file.
- The binary contents of the file.

Here's my controller script. It works fine with the HTML-based upload form and views. Please note that I use the $field_name variable with the value "Filedata" for matching the field name with the requirement from Adobe's FileReference documentation.

For using the controller with Flex I comment out the $this->load->view(...) and $this->load->helper(...) lines because they are only needed for the HTML views. I point the URL parameter in my Flex application to "http://www.mydomain.com/index.php/flexupload/do_upload" but it doesn't work.

Code:
<?php

class Flexupload extends Controller {
    
    function Flexupload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $field_name = "Filedata";
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['overwrite'] = TRUE;
        $config['max_size']    = '1000000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('upload_success', $data);      
        }
    }    
    
}
?>

Any ideas what I'm doing wrong? Thanks in advance.
#2

[eluser]Hermawan Haryanto[/eluser]
Is there anyway on the Flex to retrieve the error messages from the controller?
#3

[eluser]log2e[/eluser]
Good idea! Yes, I was able to get CI's error message back into my Flex application. It says: "The filetype you are attempting to upload is not allowed."

Now this is really strange because I tested the HMTL upload form and the Flex upload with the same set of JPG images. If I upload the images through CI's HTML upload form it works. As you can see from the controller script above the $config['allowed_types'] variable is set to 'gif|jpg|png'. Obviously the Flash Player reports a different file type to CI so that the MIME type of the uploaded file is not recognized properly.

Is there anything I can do about this?
#4

[eluser]Hermawan Haryanto[/eluser]
By that error, I guess the Flex uploader part is not uploading the file in the correct mime type then? For a quick fix, just make a print of the uploaded mime type on your upload controller:

print $_FILES['Filedata']['type']

Then put the result to the /config/mimes.php, on the gif, jpg and png.

Let me know how it goes ok?

Cheers,
Hermawan Haryanto
#5

[eluser]log2e[/eluser]
If I extract the MIME type by

Code:
$data = $this->upload->data();
$mimeType = $data['file_type'];

I get "application/octet-stream" - no matter if it is a JPG, GIF or PNG file. I followed your advice and added this MIME type to the three image formats in "/config/mimes.php", and now it works!

Btw, I also tested the file upload with the upload class from verot.net. This script also reports "application/octet-stream" as the MIME type. So I guess it's not a PHP or CI issue. But who is the culprit here? The FileReference class, the Flash Player, or the browser? Or is it a server-side setting?

Thanks for your help!
#6

[eluser]Hermawan Haryanto[/eluser]
I'm not really sure about that. My employer is using Apple Mac OS X and I use PC. Sometime we have some difficulties on that one, I uploaded using Firefox browser and it does alright, and when he upload a file using Safari on his Leopard, it's just don't work, same error as yours. So I'm not sure if it's on the server but we got to check again tho
#7

[eluser]log2e[/eluser]
The file upload works now, but the downside is that the "image_width" and "image_height" properties are empty. I took a look at the "Upload.php" library. The function set_image_properties() depends on the is_image() function, and the is_image() function depends on the correct mime type.

CI's upload library tries to detect the mime type by these two lines:

Code:
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
$this->file_type = strtolower($this->file_type);

According to the PHP documentation "$_FILES['userfile']['type']" is a piece of information that comes from the browser (PHP itself does nothing to verify this information).

The only workaround I can think of is to write a function that derives the MIME type from the file extension instead, and then to use the PHP functions exif_imagetype() and getimagesize() for further validation and getting the width and height of the image.
#8

[eluser]log2e[/eluser]
Btw, the true culprit here seems to be Flash/Flex and the FileReference class. Every upload comes through as "application/octet-stream" (see here).
#9

[eluser]jeffpeck[/eluser]
May I ask, how did you get the error message back in Flex? I am trying the same thing, but am unable to find where the output of the upload through the FileReference object is to be found.




Theme © iAndrew 2016 - Forum software by © MyBB