Drupal htaccess

Additional .htaccess settings for Drupal

Here are some extra .htaccess commands to add to the standard Drupal setup Allow access for SSL LetsEncrypt.com If you are using LetsEncrypt, the default security stops access for folders starting with a period RewriteCond %{REQUEST_URI} “/.well-known/” RewriteRule (.*) $1 [L] Force SSL RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Redirect to non-WWW RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301] Redirect images folder to default images This means that you can use shortened URI…

Continue reading

Drupal

Delete unused Drupal Modules

If you have uploaded a heap of modules into your Drupal install and want to delete the unused modules from the Hard Drive, this can work. cd sites/all/modules rm -rf `drush pm-list –status=’not installed’ –pipe` In this case it assumes the contrib modules have been installed to the sites/all/modules folder. They can also be in sites/default/modules or other locations.

Continue reading

Drupal

Playing with Files in Drupal

Drupal has file URI of the format similar to public://folder/filename.ext Where there are several datasets public private temporary There are several functions that are useful to know; The local path in the local File System $path = drupal_realpath($uri); The external URL referenced from the web server $url = file_create_url($uri); There are more in the reference.

Continue reading

Drupal

Processing File Contents in Form Uploads

Suppose you want a form that allows the user to select a file. We want to upload that file and then process its contents. First define the file in the form definition function // File $form[‘file’] = array( ‘#type’ => ‘file’, ‘#name’ => ‘files[import]’, ‘#title’ => t(‘Choose a file’), ‘#description’ => t(‘Input file must be TAB delimited….’), ); The important part is that the #name must be in array form. Then in the submit function,…

Continue reading

Drupal

Creating a file in the Drupal file system

This example shows how to create a file with a location within the Drupal file system. if (!isset($node -> uc_product_image[“und”][0])) { $filepath = “public://temp.jpg”; $data = file_get_contents($row[“frontcover”]); if (!empty($data)) { file_put_contents($filepath, $data); // echo “{$filepath}, “; $file = (object) array( ‘uid’ => 2, ‘uri’ => $filepath, ‘filemime’ => file_get_mimetype($filepath), ‘status’ => 1, ); $file = file_copy($file, “public://productimages/{$id}.jpg”); $node -> uc_product_image[“und”][0] = (array)$file; unlink($filepath); } } Done

Continue reading