I have a module which acts as a connector between a mobile application and my Drupal site.
The connector code accepts a singular (never multiple) image file (
$_FILES['file']) uploaded from the mobile app and processes it before deciding whether or not to save it to an instance of a custom entity.
The custom entity is defined in a Drupal module which also has the option on the Drupal site to upload and save an image to an entity instance. This works with no deletion issues.
If the image should be saved, it does the following (
$imgname has been defined elsewhere):
$path = "public://my_subdirectory/"; $file = File::create([ 'uid' => \Drupal::currentUser()->id(), 'filename' => $imgname, 'uri' => $path . $imgname, 'filemime' => $_FILES['file']['type'], 'filesize' => $_FILES['file']['size'], 'status' => FILE_STATUS_PERMANENT ]); $file->save(); $fid = $file->id; file_save_upload('file', , $path . $imgname);
$entity = MyModuleEntityType::create([ 'photo' => array(array('target_id' => $fid, 'alt' => null)), ]); $entity->save(); move_uploaded_file($_FILES['file']['tmp_name'], $path . $imgname); \Drupal::service('file.usage')->add($file, 'file', 'mymodule_name', $entity->id()); //this structure matches what the Drupal-side file upload does
This seems to work as it should: it creates and saves a new entity with the correct uploaded file exactly where it should be, and the files are inside the my_subdirectory folder.
$imgname is timestamp and user ID based so overlap hasn't been an issue. The files are just gone the next morning when I check the Drupal site and manually check the directory.
The entries in the
file_usage tables seem fine, the entries are indistinguishable from those created by the Drupal uploader. Status in
file_managed is set to 1. The ID referenced by the entity instance in the entity table referring to the file is correct.
/admin/config/media/file-system, delete orphaned files is set to 6 hours, but these files shouldn't be orphaned.
I tried uploading more files this morning via the mobile app in the same way, and then tried experimenting with
file_cron() by manually setting
$age to 1 second, outputting the contents of
$references. None of the files I'm testing with are returned as deletion targets.
I really don't know what else could be going wrong. I'm sure it's something simple in my file upload code above that I'm just not seeing. Do I have too many conflicting functions?
Another issue to point out is that this Drupal install is just a local copy of a Git repository, so the settings.php directories are formatted for Linux when I'm using a Windows system, and that's giving me file system errors in
/admin/reports/status. This seems to only be affecting the private system path, which I'm not using, and the configuration directory, which both can be changed in settings.php -- I'm attempting to update settings.php, but this is not being reflected in
/admin/config/media/file-system even after clearing cache. Could that be related?