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.
Assume the following excerpt of a file object:
[filename] => filename1
[uri] => public://path1/filename1
[source] => upload
[destination] => path1/filename1
Assume the following code and the destination file exists:
$source = 'public://path1/filename1';
$destination = 'public://path2/filename2';
$file = file_move($source, $destination);
The returned file object will look like:
[filename] => filename1
[uri] => public://path2/filename2_n
[source] => upload
[destination] => path1/filename1
The file_move() code
elseif ($replace == FILE_EXISTS_RENAME && is_file($destination))
attempts to change the filename property but fails because is_file() does not recognize Drupal's stream wrapper.
Is the File API intended to keep the properties of the file object in sync? If so, should this function update the filename and destination properties in this case? i did not notice a simple equivalent to is_file() in the File API. Perhaps this would be useful here.
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedthanks for the report, i'll take a look at this.
Comment #2
solotandem CreditAttribution: solotandem commentedAny word on this?
Comment #3
gaele CreditAttribution: gaele commentedComment #4
vegantriathleteNote: This is still a bug as of Drupal 8.
Another way to state the issue is that when you do a file_move with FILE_EXISTS_REPLACE the uri in file_managed does not get updated. It does get updated when you use a FILE_EXISTS_RENAME.
The file itself does actually get move to the correct directory; it also gets the desired name.
Comment #5
vegantriathleteI think the title is incorrect. I think things do work when using the default (FILE_EXISTS_RENAME). The bug is when using FILE_EXISTS_REPLACE. If I'm mistaken in changing the title, then just change it back and I'll open a new issue.
Comment #7
vegantriathleteI am coming back to this. I've read the issue summary more clearly and I think the OP did intend for this to be against FILE_EXISTS_RENAME. I can't say whether this is still an issue in 7. I don't think it is an issue in 8, though.
I'm opening a new issue for the FILE_EXISTS_REPLACE.
Comment #8
ktrev CreditAttribution: ktrev commentedI am using Drupal 8.4.4
When used with hook_entity_insert, file_move is working as expected.
But when using inside hook_entity_update it is not updating the path in the file_managed table. But the file is getting replaced.
the code at both places is:
Comment #9
msankhala CreditAttribution: msankhala as a volunteer and at Srijan | A Material+ Company commentedUpdated issue summary:
to