IMCE's API has the ability to let other module's affect the listing of available files. FileField Sources currently attempts to make use of this and hide files that aren't valid. However, the implementation is imperfect and IMCE will still show a lot of files that are not referenceable (files MUST be hook_file() aware, currently only FileField implements this functionality).

It would also be preferable to hide directories that will not contain referenceable files, such as the "css" or "imagecache" directories.

Comments

frega’s picture

caveat: i am no cck-ninja at all and the file-stuff seems pretty hard-core (so many use cases and dependencies to consider) ...

That said - would it maybe make sense to create the "file"-entries in the database "on-the-fly" if needed as they are selected rather than filter them in the IMCE module?

I created a small patch for the filefield_source_imce_value()-function that handles "The selected file could not be used because the file does not exist in the database."-error by creating file-objects/db-entries on the fly and assigning them to cck-filefield / imagefield.

It does not work flawlessly yet (the filefield validation is kinda tricky to work around) and I currently end up creating to entries in the database for each file twice, which is sub-optimal ... but could probably be resolved with a little more hacking ....

If you think this a worthwhile path to pursue, I'd work on that (maybe open a new issue) and attach the patch ... but maybe that's a totally the wrong way to go about things ... e.g. mixing files handled by "filefield"/file and non-filefield files in one folder on the server could not be looked well upon etc.

Best,
fredrik

quicksketch’s picture

Ha, I just suggested the same thing in #438940: Add ability to use file uploaded via FTP. That's definitely something to consider, I just need to figure out how this should be done properly. I think a better approach would be some kind of file scan during cron, and we'd need to maintain a database table of which files are being managed by by this automatic file scan. This way we could prevent the files from being deleted when their parent nodes are deleted. See my explanation in #438940.

frega’s picture

thanks for the reply. reading that thread i also understand why the filefield validation was doing ref-counting :) - i'd still prefer to be able to do this on-the-fly (using maybe the same "protect"-these-files-from-being-removed-table), as i have a project where several thousands of files would need to be sync'd ... I'll see whether i can hack on that, and post the stuff in the other issue which seems a little more on-topic for that :)
best,f.

momper’s picture

subscribe

mani.atico’s picture

subscribe

geerlingguy’s picture

Subscribe.

emptyvoid’s picture

Although the blog post exclusively uses Drush, could we pull some knowledge from the technique used and build a simple settings page using the FORM API for administrators to:

a) Register the path
b) Run an audit
c) Run an update adding all files not found in the DB?
d) Run an update to clean files that are referenced in the db but point to a missing file?

http://www.freelock.com/blog/john-locke/2010-02/using-file-field-importe...

quicksketch’s picture

c) Run an update adding all files not found in the DB?

I should note that this is decreasing in importance now that IMCE registers its files upon upload in IMCE 2. This means that all future files uploaded by IMCE are now all usable through FileField, since they are now in the database. We're discussing options for better IMCE integration in #877452: Enable uploading and deletion through IMCE.

danny englander’s picture

Subscribing

acondiff’s picture

any update on this issue? I am actually trying to do the same thing. I too had the same idea to write selected files to the database on-the-fly. Any updates would be appriciated. frega, you said you had a patch for this. if it is working I would definitely be interested in it.

If not I can give it a shot myself although I am not too experienced in this.

Thanks.

acondiff’s picture

So I found a solution. In imce.inc in the filefield_sources module, instead of this:

  foreach ($directory['files'] as $filename => $file) { 
    if (!isset($db_files[$filename])) { 
			unset($directory['files'][$filename]);
			$directory['dirsize'] -= $file['size']; 

    } 
  }
  return $directory;
}

put this:

  foreach ($directory['files'] as $filename => $file) { 
    if (!isset($db_files[$filename])) { 
			//unset($directory['files'][$filename]);
			//$directory['dirsize'] -= $file['size']; 
			$mime_type = file_get_mimetype($filename);
			$sql = "INSERT INTO files(uid, filename, filepath, filemime, filesize, status, timestamp)
			VALUES('1', '" . $filename . "', '" . $sql_dir_name. "/" . $filename . "', '" . $mime_type . "', '" . $file["size"] . "', '1', '" . $file["date"] . "')";
			//echo $sql . "<br><br>";
			db_query($sql);
    } 
  }
  return $directory;
}

So this basically creates a database entry for new files that are not in the database. So now when I click insert it inserts the image, although, whenever I go to save my node I get this error: "Referencing to the file used in the field is not allowed."

Does anyone know what is wrong here?

Thanks.

acondiff’s picture

Ok I found a solution for that as well so it does not throw the error. In the filefield module, I just commented out this block of code and it works like a charm.

if ($references['filefield'] == 0) {
          form_error($element, t('Referencing to the file used in the %field field is not allowed.', array('%field' => $element['#title'])));
}
nuthman’s picture

The code mod works for me to list and insert the file, but when I hit save, I get the following error:

Referencing to the file used in the About Image field is not allowed.

(About is the name of my filefield)... Is something left of of the database? Is this a permissions issue?

acondiff’s picture

You need to comment the following code out in the filefield module:

if ($references['filefield'] == 0) {
          form_error($element, t('Referencing to the file used in the %field field is not allowed.', array('%field' => $element['#title'])));
}

I have heard this is the code to comment out in other versions:

if (field_file_references($file) == 0) {
form_error($element, t('Referencing to the file used in the %field field is not allowed.', array('%field' => $element['#title'])));
}

This will get rid of that message and allow you to continue without error. Hope that works out for you.

quicksketch’s picture

You need to comment the following code out in the filefield module:

Please stop suggesting changes that open up security vulnerabilities in FileField module. IMCE (1.x) as it is a security risk by itself (at least that it allows deleting of any file on your Drupal site), this change would allow a malicious user to delete any file within your Drupal site by attaching it and then deleting the node.

acondiff’s picture

Oh, I'm sorry. =)

I was just trying to help. This worked for me. I am actually pretty new to Drupal and I was totally unaware. What would be a better solution?

baff’s picture

subscribe

RavenHursT’s picture

Status: Active » Needs review
StatusFileSize
new1.07 KB

Here's a patch for filefield_sources.

Please Review.

Need something better than just commenting out that line in filefied.

jeffwidman’s picture

subscribe

TripX’s picture

Component: User interface » General

+1

Happens here too for D7 version (1.3)

TripX’s picture

Happens in 7.x-1.4 when I want to add a picture through IMCE - following message appears: "The selected file could not be used because the file does not exist in the database."

quicksketch’s picture

Title: IMCE lists unusable files » IMCE lists unusable files (The selected file could not be used because the file does not exist in the database)

Updating the title for other users hitting this.

_petja’s picture

+1
IMCE is totally unusable in D7 version because of this.

edit. Spoke too soon... seems that this happens with image field. Haven't had probs with file field so far. It would be nice if IMCE browse worked with image field type too though.

quicksketch’s picture

_petja: So far you can only reference files within the same field. So if you uploaded all your files into the image field, they'd all work there but not in the file field. Files right now are field-specific.

_petja’s picture

Didn't know that. Thanks!

paskainos’s picture

subscribing

EDIT: The security issue notwithstanding, @RavenHursT's patch here combined with @ufku's patch here seems to present an ideals solution.

ItangSanjana’s picture

The problem still persist with image files (7.x-1.4 version).

anthonyR’s picture

I'm also experiencing this problem with image files in combination with Filefield Sources 7.x-1.3

dwalker51’s picture

+1

dbusu’s picture

+1

serialbob’s picture

Version: 6.x-1.x-dev » 7.x-1.4
StatusFileSize
new49.61 KB

Hi,
It's the same think for me.
I had created a content type, with 2 additional fields (file, and image_thumb). When i browse server, and when i select the image, the error appear (cf image capture).
I tried to use different file manager, such as elfinder, imce,... and the same problem is here !
I use the Drupal V7.2.
Thanks in advance.

deshiknaves’s picture

subscribing

deshiknaves’s picture

I'm getting the same error message, but these files do exists in my database. All of these images are stored in files_managed. Yet when I add them, I get an error. I can see that the images uploaded with IMCE are not in the database, but the files that I'm trying to reference are one that I have uploaded through Drupal 7 file uploader itself. Yet, I'm getting an error for the file not exisiting. Auto complete method works fine, but not through IMCE file browser. Why would this be?

deshiknaves’s picture

Correction to my last post. Actually IMCE is adding the entries to files_managed and files_used for its images.

deshiknaves’s picture

I found that my issue was that function filefield_source_imce_value($element, &$item)

Was defining the uri by:
$uri = preg_replace('/^' . preg_quote('/' . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_imce']['file_path']);

But the preg_replace would not happen because the base path is /site_name/ then $file_directory_prefix.

So to make it work, I added global $base_path; and replaced the preg_replace with:

$uri = preg_replace('/^' . preg_quote($base_path . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_imce']['file_path']);

Has to be defined as global $base_path; at the top of the function

With IMCE 7.x.1.4 all the images uploaded with IMCE is stored in files_managed and so they can all be used. With the addition of $base_path, it is completely usable and works as expected.

dbusu’s picture

comment #35 solved the problem for me. Thx :)

ShadowMonster’s picture

In my case I found that $uri is wrong and i was something like /system/files/3/photos/somefile.jpg so i make small fix:

just add after uri pre_replace:

$uri = str_replace('/system/files/', $scheme . '://', $uri);
ShadowMonster’s picture

Anyway I do not know if this is right because system is using file directly from /system/files/3/photos/somefile.jpg and not from predefined by filepath

deshiknaves’s picture

$file_directory_prefix should add in /system/files/ there. What does just $file_directory_prefix output for you?

ShadowMonster’s picture

It return root path to files folder as ex:

/home/domain/files

My files are outside public_html and set as private.

Cebra’s picture

Are there any new fixes? I got the same problem and I need a solution.... it is urgent. :-(

svendecabooter’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Component: General » Source: IMCE
StatusFileSize
new1.04 KB

Attached is a patch for the solution proposed in #35 by deshiknaves, which works fine for us.

ts145nera’s picture

subscribe
#42 work for me
Thank you

deshiknaves’s picture

Thanks for making the patch, I should read up on how to make patches. Cheers.

thekevinjones’s picture

#42 worked for me

thanks

hepabolu’s picture

#42 works if (a) there are no language prefixes between the $base_path and the $file_directory_prefix and (b) IMCE needs to serve up public files. Somehow I have a language prefix in the path and the $file_directory_prefix doesn't take into account that private files have a path containing 'system/files' as 'root' of the file directory.

I'm not sure how to create a patch, so I'll explain below. I've replaced the line

$uri = $uri = preg_replace('/^' . preg_quote($base_path ...........

with

$searchsrc = '/^(.*)' . preg_quote($base_path . $file_directory_prefix . '/', '/') . '/';
if ($scheme == 'private') {
	$searchsrc = '/^(.*)' . preg_quote('/system/files' . '/', '/') . '/';
}
$uri = preg_replace($searchsrc, $scheme . '://', $item['filefield_imce']['file_path']);
forexpivots’s picture

Hi guys,

I don't know how to apply a patch, I've tried to do it manually but I've just made bullocks of the file...

Could some of the gents (lasses) here post the modified imce.inc file?

I'd really appreciate...this would same me a few more grey hairs.....

Thanks!

forexpivots’s picture

I've applied the changes that Helma has mentioned at #46
It worked perfectly.

I've found it first on his blog here:http://www.sourcefusion.nl/cms/content/imce-filefield-sources-private-an...

Thanks

quicksketch’s picture

#42 (and subsequent comments) are referring to a separate problem with IMCE that should be discussed/fixed in #1183866: IMCE integration doesn't work when Drupal is installed in a subdirectory.

kerios83’s picture

- subscribe +1

ufku’s picture

I've created a sandbox project to solve all IMCE-filefield issues. It's a standalone module independent of filefield sources. I may promote it to a full project if it gets enough attention.

donquixote’s picture

See also
#1392438: file_managed or not?
(imce queue)

amaisano’s picture

I have this issue with my D6.2x site. Is there a patch for D6? The code is a bit different for the 6x version of this module... Thanks.

dimitriseng’s picture

I have tried the imce_filefield module and it looks like it is fixing the issues reported in this issue. Are you considering in using that module or planning to fix this within FileField sources?

BioALIEN’s picture

Priority: Normal » Critical

I came across this thread when searching for a similar issue. I believe the priority here should be raised to put this on the radar. Here's my feedback:
imce_filefield.module does indeed fix the issues reported here. After extensive testing on D6, here are my thoughts:

+1 to renaming 'IMCE file browser' to 'Filtered IMCE file browser' (Enabled sources).
+1 to porting imce_filefield patches to filefield_sources under 'Full IMCE file browser' (Enabled sources).
+1 to limiting IMCE to the 'File path' (Path settings) in 'Filtered IMCE file browser'.
+1 to remove the ability for add/remove files in IMCE in line with existing implementation in 'Filtered IMCE file browser'.

The above will give the community the functionality they need with the least amount of refactoring. This will make filefield_sources feature complete with regards to integration with IMCE and we'll have one less module to install. I recommend ufku be made a co-maintainer of filefield_sources to help with the above.

grabby’s picture

I was experiencing this with the private file system in 7.12. I tried #46, though the line that’s supposed to be replaced,

$uri = $uri = preg_replace('/^' . preg_quote($base_path ...........

is

$uri = preg_replace('/^' . preg_quote('/' . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $item['filefield_imce']['file_path']);

in my instance (imce.inc of FileField Sources 7.x-1.4). Anyway, it works despite getting

Notice: Undefined variable: base_path in filefield_source_imce_value() (line 135 of \sites\all\modules\filefield_sources\sources\imce.inc).

each time I insert a file. Did I do something wring? My Drupal is not in a subdirectory.

mattlc’s picture

Hi all,

I want to be able to dynamically add files (images) to managed_files.These files come from an FTP repository in sites/default/files/[repository].
After having applied "patch" proposed here, I ran into present bug troubles.

I finally got throuh it by hacking "imce.inc" file :

I replaced the line

form_error($element, t('The selected file could not be used because the file does not exist in the database.'));

by :

$filepath = $uri;
		$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
		// Check that the destination is writable.
		$directory = $element['#upload_location'];
		if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
			watchdog('file', 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $filepath, '%destination' => drupal_realpath($directory)));
			drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $filepath)), 'error');
			return;
		}
		// Clean up the file name extensions and transliterate.
	    $original_filepath = $filepath;
	    $new_filepath = filefield_sources_clean_filename($filepath, $instance['settings']['file_extensions']);
	    rename($filepath, $new_filepath);
	    $filepath = $new_filepath;
	    $validators = $element['#upload_validators'];
	    // Save the file to the new location.
	    if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
	      $item = array_merge($item, (array) $file);
	
	      // Delete the original file if "moving" the file instead of copying.
	      if ($instance['widget']['settings']['filefield_sources']['source_attach']['attach_mode'] !== 'copy') {
	        // TODO: Figure out a way to re-introduce this feature. By unlinking the
	        // file it causes the option to be removed from the select list on the
	        // form rebuild, thus throwing an illegal option error.
	        //@unlink($filepath);
	      }
	    }
	
	    // Restore the original file name if the file still exists.
	    if (file_exists($filepath) && $filepath != $original_filepath) {
	      rename($filepath, $original_filepath);
	    }

This code comes from attach.inc.

The resulting behavior is : when the selected file isn't a drupal managed file, it saves the file as it in the right target directory and attaches it to the node.
I now have to improve it so that the file won't be located both in the repository and in the "field targeted" directory while allowing other fields in other content types referencing it.
This could be done by a good files directories architecture in fields like this one :
- image_repo
- bikes (fields related to bikes)
- boats (fields related to boats)
- cars (fields related to cars)

Forcing user to lookup files in "field directories" could be done by deleting the original file in "image_repo".

More feedback soon.

quicksketch’s picture

Category: bug » feature
Priority: Critical » Normal
Status: Needs review » Closed (duplicate)

This is pretty well all covered by #877452: Enable uploading and deletion through IMCE, which will be in 1.5. It adds the ability to attach files that are in different fields and to use the global IMCE preferences instead of the ones FileField Sources provides. This never was a "bug", it's functioning the way it was written to function, however that didn't line up real well with people's expectations of IMCE. In any case, please try out the 1.5 version when it's available (or try the dev version now), and open a new issue with any further requests.