I read the other 2 threads relating to this issue and I think mine is a little different. I uploaded a file through the image field, It is saved in the author-named folder I have specified. I try to use the File browser option to select it for another node. I get the error. Is this the same issue? I should think it's in the DB if I uploaded it through the node in the first place, right?

A further frustration - I swear it worked once then stopped working. In fact, inspection of the code of the 2 nodes shows that they have the same filename, rather than file.jpg and file_0.jpg - so it must have worked.

CommentFileSizeAuthor
#8 imce.inc_.patch1.11 KBufku
#4 imce.inc_.urldecode.patch762 bytesufku

Comments

Poieo’s picture

I'm having a similar issue, and I think it may be related. It seems you can only reference files that were added before your referencing field.

If you create a node, add a file via filefield, then save that node...you can then create another node containing a filefield sources field and reference that field.

However, if you create a node containing a fieldfield sources field first, then create a node with a field you wish to reference, you cannot edit the node containing the sources field and add the file you just created.

I'm not sure if this is clear, but I had the problem with the autocomplete field and the file browser.

dwong127’s picture

I've had the same issue attempting to reference files with spaces in their name. The problem seems to be in the filefield_source_imce_value() function in imce.inc: it does a query based on the filepath, but the incoming filepath is URL-encoded (spaces are replaced by "%20", etc.) and this causes the $file_path variable to not match what's stored in the files.filepath database column. I think that a call to urldecode addresses the issue.

Updated function:

function filefield_source_imce_value($element, &$item) {
  if (isset($item['filefield_imce']['file_path']) && $item['filefield_imce']['file_path'] != '') {
    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
      $file_path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '', $item['filefield_imce']['file_path']);
    }
    else {
      $file_path = preg_replace('/^' . preg_quote(url('system/files'), '/') . '/', file_directory_path(), $item['filefield_imce']['file_path']);
    }

    // Resolve the file path to an FID.
    if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", urldecode($file_path)))) {
      $file = field_file_load($fid);
      if (filefield_sources_element_validate($element, (object) $file)) {
        $item = array_merge($item, $file);
      }
    }
    else {
      form_error($element, t('The selected file could not be used because the file does not exist in the database.'));
    }
    // No matter what happens, clear the value from the file path field.
    $item['filefield_imce']['file_path'] = '';
  }
}
quicksketch’s picture

Category: support » bug
Status: Active » Needs work

Ooo, nice find dwong127. A patch would make these changes easier to review, but this sounds promising.

ufku’s picture

Status: Needs work » Needs review
StatusFileSize
new762 bytes

Here is a patch that uses rawurldecode as IMCE uses rawurlencode for encoding file names.

brian_c’s picture

Priority: Normal » Critical

Can confirm that the current release's file browser only works on files with no spaces in the name.

We have applied ufku's fix (using rawurldecode() on $file_path) and can confirm that it works.

quicksketch, any chance of a reasonable assurance that the next release will have this issue addressed? Would just like to know if we need to worry about updating to a later version in the future. :)

Seemed appropriate to bump this one up to Critical, spaces are everywhere in this crazy post 8.3 world. ;)

Tails’s picture

Category: bug » support
Priority: Critical » Normal

I get the same error, even with the patch applied.
Is the module supposed to be able to select images that have not been used before?

What I am trying to do is to upload an image via FTP, and then to use it with FileField.

[edit] whoops, I accidently changed the issue settings... I thought it was post specific.

czperry’s picture

Category: support » bug
Status: Needs review » Active

I applied the patch and was getting the same error too.

I tracked it down to the fact that IMCE absolute urls was turned on. The database query to look for the uploaded file needs to strip out the base path.

ufku’s picture

Status: Active » Needs review
StatusFileSize
new1.11 KB

Additionally disables absolute URLs.

jordana’s picture

This indeed seems to works very well!
Thanks very much!

@ #6 Tails - check out this thread about using files you've uploaded through FTP:
http://drupal.org/node/438940
I've tried the patch and it works well enough for me.

jackalope’s picture

Had the same error due to spaces; the patch in #8 took care of the error. Thanks!

aaronbauman’s picture

Status: Needs review » Reviewed & tested by the community

#8 is go!

heavy_engineer’s picture

Hi

Tested on Pressflow 6.15.73 AOK as well (as to be be expected)

Great module. thanks.

quicksketch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks ufku! Finally committed.

Status: Fixed » Closed (fixed)

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

david3000’s picture

Hello guys, I have this same issue "The selected file could not be used because the file does not exist in the database" when I try tu set set an image from the browser. I tried all these patches but nothing, allways the same. All the permitions are set right on my server by the way. I never had this issue before in others modules or patches, just in this one. Also, when I use the IMCE module alone works ok, for example uploading by my account. I really need a little help about this. Thanks in advance.

acondiff’s picture

i need some help too. I am also experiencing this issue. I commented out the code that filters files visible to imce that are not in the database because I would like to use those files. The need to be added to the database on-the-fly or as they are selected for use in the node. Any help would be amazing.

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'])));
}
pianomansam’s picture

Priority: Normal » Major
Status: Closed (fixed) » Needs work

This solution seems to be working for me, but as it hacks filefield, it should only be a temporary solution. We need to find a better way to get this working again.

pianomansam’s picture

Priority: Major » Critical
cheyer’s picture

subscribe

Luciuz’s picture

subscribe

fugazi’s picture

subscribe

vrajak@gmail.com’s picture

Looking for a fix, and this seems to be related: http://drupal.org/node/441280

Quicksketch strongly recommends against doing what is posted in comment #18. See http://drupal.org/node/441280#comment-2884196

soyyo’s picture

subscribe

Crom’s picture

subscribe

quicksketch’s picture

Component: Code » General
Priority: Critical » Normal
Status: Needs work » Closed (fixed)

Please do not reopen this issue, the original problem has been solved.

For IMCE users who get this error when selecting a file, see #436182: IMCE lists unusable files (The selected file could not be used because the file does not exist in the database).

pianomansam’s picture

Status: Closed (fixed) » Closed (duplicate)

quicksketch, I do not think it is appropriate to label this issue as closed and fixed because the solution provided in #436182: IMCE lists unusable files (The selected file could not be used because the file does not exist in the database) has not been committed. Labeling as a duplicate probably makes better sense.