I'd like to use IMCE to attach files to commerce products.
I have a lot of files on the server so I wrote a script using the file API ( file_save_data ) and it does copy the file to my public files area and add it to the files_managed table.

When I browse to the directory with the managed files using IMCE ( from the commerce product attach ) I do not see the managed file ? I'm not sure if I'm missing a step ?

Comments

dman’s picture

Yes.
I did the same yesterday.
You need to not just register the files using file_save(), you ALSO need to tell the system that the file is "in use" otherwise the IMCE browser won't let you touch them (it actually finds them, then hides them from you in the filebrowser)

Based on a good (D6) start at fixing this issue at
http://www.freelock.com/blog/john-locke/2010-02/using-file-field-importe...
I discovered a lot of interestingness with the D7 file management routines, and came up with http://drupalcode.org/project/file_ownage.git/blob/refs/heads/7.x-1.x:/f...
- if you look at that and compare with what you've done, you'll probably see the extra step - in the mass of comments here;

        drush_log('Saving file to database: '.$file->uri, 'notice');
        // Get file wrapper CRUD to save it for us
        drupal_chmod($file->uri);
        file_save($file);
        // Other modules - specifically filefield_sources -
        // May not play ball unless the file is 'in use' as well.
        // @see file_managed_file_validate()
        // @see file_usage_list($file);
        // We don't have anything useful to tell it, about previous usage
        // so just say it's managed by 'system'
        // Now the filefield_sources imce browse will at least be able to find these.
        file_usage_add($file, 'system', 'file', $file->fid);
        // Even *with* this, it still won't be available to the "Reference existing" option
        // Until used at leased once *by the same field*

        drush_log("Saved file to database: file: {$file->uri} fid:{$record->fid}" , 'ok');

(I'm trawling the issues right now looking for a nice solution to the second problem - "reference existing". I can fix it with a minor patch to filefield_sources itself, but am trying to find the rationale first.

chaloum’s picture

I have the same issue IMCE shows the files when accessing if from the WYSIWYG editor but from Drupal commerce It doesn't display the files

fietserwin’s picture

Status: Active » Needs review

I have the same problem, but only in the root directory (sites/default/files). Thus not related to files not being managed but an error in the source code.

file sources/imce.inc, function filefield_source_imce_custom_scan():

  $sql_uri_name = $dirname == '.' ? $scheme . '://' : $scheme . '://' . $dirname;

  $result = db_select('file_managed', 'f')
    ->fields('f', array('filename'))
    ->condition('f.uri', $sql_uri_name . '/%', 'LIKE')
    ->condition('f.uri', $sql_uri_name . '/%/%', 'NOT LIKE')
    ->execute();

In the root directory, this will lead to select ... where ... LIKE 'public:///%'. So code should be something like:

  $sql_uri_name = $dirname == '.' ? $scheme . '://' : $scheme . '://' . $dirname . '/';

  $result = db_select('file_managed', 'f')
    ->fields('f', array('filename'))
    ->condition('f.uri', $sql_uri_name . '%', 'LIKE')
    ->condition('f.uri', $sql_uri_name . '%/%', 'NOT LIKE')
    ->execute();

Sorry for not posting as a patch, but this is very simple to fix.

tekante’s picture

Attaching patch based off of #3 which worked for me. It's the same as #3 describes with one adjustment to the not like condition to help make sure what is excluded is in a subdirectory ('_%/%' as a pattern instead of just '%/%'). Records were found in our system like public:///filename.txt for the URI when in the root directory and without the _% part of the pattern they were being excluded.

fietserwin’s picture

Category: support » bug
Status: Needs review » Reviewed & tested by the community

Thus: confirmed as being a bug and tested by multiple people => change category and status.

quicksketch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks guys, looks great! Seems like this doesn't affect the D6 version, so committed to D7 only.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

added some more details