Skip to main content

File

Drupal File Operation

Submitted by system on
This post will illustrate various handy file operations available in Drupal Get Absolute Path of a file from URI : drupal_realpath() A URI in drupal looks like: private://donor_registration/somefile.csv for a private file Absolute Path: /srv/www/example.com/public_html/sites/default/private/files/donor_registration/somefile.csv <?php $absolute_path = drupal_realpath($uri); drupal_set_message(t("Realpath is: $absolute_path"), 'status', FALSE); ?> -- Sources: https://api.drupal.org/api/drupal/includes!file.inc/function/drupal_realpath/7

Programmatically Saving File

Submitted by amitsedai on
Get the filename URI path from the dir_name located under public files directory. Save data in the destination. If file exists with the name, replace Get the download link. <?php $dest = file_build_uri('dir_name/'.$filename); $file = file_save_data($data, $dest, FILE_EXISTS_REPLACE); $download_link = file_create_url($file->uri); ?>

Update File Path in Drupal

Submitted by amitsedai on
We have a site that we had been migrating since Drupal 4. Few days back when we tried to migrate the same from Drupal 6 to Drupal 7. There was an issue with respect to file location that was present since previous upgrades. To resolve that a symlink was created called "files" with linked to sites/default/files during D6 upgrade. This led to first loss of many files as Drupal does not handle symlink based file saving well. Secondly the files created were saved under public://files/filename.ext instead of public:://filename.ext which caused created files not being accessible.