Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When dealing with files using IMCE, interaction with the FILES table is made.
The information that is passed to the developer contains information of the file, but
not the ID of the file. So if you want to make any change of this file in the files table
you have to query to the path column, wich generates a very slow performance query.
Here is a simple patch to get this FID extra information to uploaded files.
Comment | File | Size | Author |
---|---|---|---|
#2 | imce.file_.id_.patch | 2.08 KB | ufku |
#1 | add_fid_info2.patch | 3.89 KB | savioret |
add_fid_info.patch | 1.85 KB | savioret | |
Comments
Comment #1
savioret CreditAttribution: savioret commentedHere is the full patch, previous was not complete.
There is a problem, and it's that when you open the IMCE window and you have to load the current directory, you have to fetch the images information.
Thus, to get the FID of each file it should to query the files table using the path column... and that's exactly what we're trying to avoid... so in this case the FID is 0.
This patch is useful when you upload a file for first time. You have the FID, and you don't need to query to the path column.
In other cases, imce just works as always.
In my case, I just use IMCE to upload to a temporary folder, and the files are always new uploads.
With this modification, I have never to query the path column to get the file row of the files table, wich is not indexed.
Hope it helps to anybody.
Comment #2
ufku CreditAttribution: ufku commentedThanks for the patch. I've made one that does not require to patch template files. It introduces imce.urlId object that stores file paths(urls actually) with their ids, so we can grab the id using the file path.
Comment #3
dragon2000 CreditAttribution: dragon2000 commentedyour patch works correctly for the new uploaded file.
but it doesn't work for already existing files.
Comment #4
ufku CreditAttribution: ufku commentedThe patch does aim new inserted files. Other files are read from the file system(this is required to support ftp uploaded files) and it is not feasible to get their IDs by querying the database for each.
Comment #5
ufku CreditAttribution: ufku commentedThis has been committed with an extra relpath property in the file object.
It is the rawurlencode'ed file path relative to the drupal file directory path.
This is how it can be used to get the real file path with file.relpath:
$filepath = file_directory_path() .'/'. rawurldecode($imce_relpath_variable);
This method does not require checking download method or clean urls and will give more accurate results than using file.url since file.url may contain double encoded characters such as & or #.
Caveat the resulting filepath may contain non-utf8 characters and will not match the path stored in the DB because the file names are read from the file system which may store names in its own locale.