form_fields config confusion

edited October 2011 in Modules
I want to configure a form_fields function such that it can upload an image (a screenshot) and videos (perhaps of several types, eg m4v, ogg, webm) in one submit. However, I'm a bit confused as how the various configuration arrays enable this. It seems there are a number of files that might require editing outside of the model I'm concerned with - eg in the application/config folder I might need to edit: mimes.php, MY_fuel.php and asset.php. Also, I'm not clear how the "assets_server_path" function really works. For example, I can upload images OK with only 1 argument ( assets_server_path('_uploads/photos/') ). If I can determine the path completely with that, why is there a 2nd argument that seems to determine the parent folder ( eg assets_server_path('_uploads/photos/', 'images') )? What is the relationship between assets_server_path() and editable_asset_filetypes, or, how do you determine what extensions are permitted by the upload? How are the keys of $config['assets_folders'] usable, and do they have a bearing on "assets_server_path"? I should add that I've succeeded with uploading images, and video, but not adequately determining which video file types are permitted, and where. I can upload mp4 extensions, but not m4v, even thought the configuration for both seems in hand!

I've tried to discover the answers for myself from the forums and documentation, but I can't seem to get a cohesive picture!

Comments

  • edited 6:19PM
    In your situation, the 'editable_asset_filetypes' config parameter will need to be changed to include those mime types for your directory "_upload". This is used by FUEL to make sure that the proper file types are being uploaded to the correct folders. Also, it looks like m4v is not in the CI mime types config file so that will need to be added as well.

    Additionally, if you want to add a folder like "_uploads" to your assets, you will need to add that to your "asset_folders" asset config parameter and make it writable (which I imagine you've already done).

    With regards to the second parameter on the assets_server_path() function, it grabs the path for that asset type from the config. If you don't change the structure of your asset directory, then you don't need to use it.

    Additionally, there is a "sanitize_images" property you can set on your module in MY_fuel_modules.php that should be probably set to FALSE, since it uses the xss_clean function which can give you false positives when uploading certain file types.

    Most all the logic for this happens in the fuel/modules/fuel/controllers/module.php in the _process_uploads() method at the bottom. In particular around line 1441 where it sets the $config['allowed_types'] configuration parameter for CI's File Upload class.

    I hope that helps.
  • edited 6:19PM
    Thanks again - very valuable advice. I think more by accident than design, I had checked all the points you recommend, but I have it confirmed now. However, having followed your instructions to the letter, I still could not upload .M4V (.MP4 no problem). But I fettled away, and it turned out to be the .M4V mime type that was at fault. I had declared it as 'm4v' => 'video/x-m4v', but by changing it to 'm4v' => 'video/mp4' everything worked OK!! The article at http://garrettstjohn.com/entry/common-issues-codeigniter-file-upload/ suggests a given extension might need several definitions (if I read it correctly). I have now used both mime definitions for .M4V and everything still works. I was using Chrome to upload the files, if that was of any interest.
  • edited 6:19PM
    Good to know about the mime-type issue and thanks for posting back.
  • edited 6:19PM
    Hi,
    I've set up my asset filetypes like so, in config/MY_fuel.php:
    $config['editable_asset_filetypes'] = array( 'images' => 'jpg|jpeg|jpe|gif', 'pdf' => 'pdf', );

    In one of my simple module forms, I want to be able to upload images, so I use an _upload form field with an $upload_path set to "images". Like so:
    $upload_path = assets_server_path('groups/', 'images'); $fields['image_upload'] = array('type' => 'file', 'label' => 'Mylabel', 'upload_path' => $upload_path, 'overwrite' => TRUE, 'filename' => '{groups_id}')

    I can upload JPEGs just fine, and if I try to upload a PNG, it fails, as it should. However, the form will accept and process a PDF. Is this normal behavior?

    I would like to restrict this particular form to uploading only JPEGs. I've tried passing an 'accept' parameter in my image_upload field, to no avail.

    Incidently, if I change 'images' to, say, 'bubbles' in the 'editable_asset_filetypes' array, JPEGs are still uploadable in the images form field. It seems that if a media type is listed in 'editable_asset_filetypes', regardless of what folder it is assigned to, it will be uploadable.

    Am I missing something very obvious?
  • edited 6:19PM
    A couple things to try out. Can you debug the value of "$config['allowed_types']" around line 1438 in the fuel/modules/fuel/controllers/module.php file to see what it outputs? Also, what happens if you add the 'images/groups' folder to editable_asset_filetypes?
    $config['editable_asset_filetypes'] = array( 'images' => 'jpg|jpeg|jpe|gif', 'images/groups' => 'jpg|jpeg|jpe|gif', 'pdf' => 'pdf', );
  • edited 6:19PM
    Regardless of the key I use in editable_asset_filetypes (i.e. what folders I add, how they are named, how many there are) all the filetypes listed as values in the array are allowed for any upload field.
    By specifying, for example...
    ... 'beeblebrox' => 'jpg|jpeg|jpe|gif', 'prefect' => 'pdf', ...
    ... in editable_asset_filetypes, I can upload jpgs, gifs and pdfs in any of my upload form fields, regardless of what parameters I use for "assets_server_path".

    So all the allowed filetypes are taken into account but they are not specific to any upload folders.

    As for debugging, the value of "$config['allowed_types']", I don't really know how to go about that, since I'm actually a designer and not a programmer. If you or anyone else can point me in the right direction, I'll be happy to work it out and get back to you.

    Thanks
  • edited July 2012
    Out of curiosity I went back to the "videos" module I was working on which sparked this thread, and for editable_asset_filetypes I have

    $config['editable_asset_filetypes'] = array( 'images' => 'jpg|jpeg|jpe|gif|png', 'pdf' => 'pdf', 'media' => 'jpg|jpeg|jpe|png|gif|mov|mp3|mp4|m4a|m4v|flv|swf|aiff|ogv|webm|mpeg|mp2', '_uploads/videos' => 'jpg|jpeg|jpe|png|gif|mov|mp3|mp4|m4a|m4v|flv|mpg|swf|aiff|ogv|webm|mpeg|mp2', '_uploads/documents' => 'doc|docx|xls|xlsx|pdf|rtf|txt|xml' );

    so in theory I should only be able to upload the values for '_uploads/videos' (this is the defined assets_server_path for the module). But in fact I can upload a PDF fine, and printing out the 'allowed_types' from the module.php controller's _process_uploads() method shows 'pdf' when I do so.

    If I remove all instances of 'pdf' from my editable_asset_filetypes then the upload is refused, but if pdf is included in any key, the upload goes ahead OK.

    So I can confirm maxbel's issue with v.0.93.
  • edited July 2012
    That is indeed a bug that looks to have been fixed in the 1.0 branch:
    https://github.com/daylightstudio/FUEL-CMS

    To fix in .93, try adding the following to the fuel/modules/fuel/controllers/module.php around line 1439 right before "$config['remove_spaces'] = TRUE;"
    $upload_folder = trim(str_replace(assets_server_path(), '', $config['upload_path']), '/'); $config['allowed_types'] = ($this->assets_model->get_dir_filetype($upload_folder)) ? $this->assets_model->get_dir_filetype($upload_folder) : 'jpg|jpeg|png|gif';
    The folders names (e.g. images/groups) should not have trailing slashes.
  • edited 6:19PM
    That did it. Thanks admin!
  • edited 6:19PM
    When i try to upload the mp4 file, error occured as No video with supported format and MIME type found.

    Can u help me.
  • edited 6:19PM
    You may need to add it to the fuel/application/config/mimes.php file
Sign In or Register to comment.