0

I currently have some working example code where when a URL path i defined using hook_menu has been passed; My custom module will use file_get_path() to pull a txt file from another ftp server. It will then return $data. The source data is stored that each line is a record. So I simply read the file one line at a time and use the explode() method to break the data into its components. From that I have it building a string in the format of a JSON at this point I return it to the user as a consumable JSON. This is used for my front end JavaScript. The problem is this data in the txt file changes very little over time. (its updated every night but very little changes).

I would like to reduce my request time by moving the fetch of the txt file to a cron job and build the JSON and save it in a sub directory of the Files directory. That way every request for the JSON data will be quick and not require a file_get_contents() and will also not require building the JSON data on every request.

I have been looking into the Files API and the hook_cron but very few quality examples show how to create a file or run a cron. I just need a simple example where a cron hook saves a simple file to a directory inside the files folder.

I'm using Drupal 7

UPDATE:

I tried the following: (objective to make sites/mysite.com/files/myDir/test.txt)

//File Name: MyModule_cron.inc
function MyModule_cron(){
    $baseDir = 'public://myDir';
    $fileContent = "hello world";
    file_prepare_directory($baseDir);
    file_put_contents($baseDir.'/test.txt', $fileContent);
}

This is loaded in from an inc file (added into the info file) and I have cleared my cashe then run cron. I then check in the sites files folder. The myDir is not created along with the file not being made. Is there something i'm missing? I'm not getting any error messages when running cron.

  • Are you sure it is being fired at all? Including files through the .info file does not work unless the file contains classes or interfaces (see this). Use module_load_include in your .module file instead. – sareed Jan 15 '16 at 19:31
  • what hook method should i call module_load_incluce() – Patrick W. McMahon Jan 15 '16 at 19:35
  • No hook. Just put it in the top of your .module file. – sareed Jan 15 '16 at 19:38
  • Thank you sareed that worked. I added the module_load_include() at the start of my myModule.module file not in any hook methods. This worked. – Patrick W. McMahon Jan 15 '16 at 19:39
3

I just need a simple example where a cron hook saves a simple file to a directory inside the files folder.

That would be literally as simple as:

function MYMODULE_cron() {
  $path = 'public://foo/bar/';

  if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
    $content = function_to_generate_content();
    file_put_contents($path . 'baz.ext', $content);
  }
  else {
    // Log an error
  }
}
  • 1
    file_save_data() could be a good option too. Depending on the specifics. – sareed Jan 15 '16 at 16:50
  • Or fopen/fwrite, or any number of other equivalents @sareed. The thing with file_save_data is it creates a file object, which the OP doesn't seem to need. This is being done in the name of performance, so creating an unnecessary object and invoking Drupal's internal APIs would be overkill (unless those are needed of course). Like you imply, need more specifics from the OP to know that – Clive Jan 15 '16 at 16:53
  • So if i understand this correct this will save a file inside the sites 'files' directory in the following files/foo/bar/baz.ext is this a correct assumption that public:// refers to the current sites files directory? – Patrick W. McMahon Jan 15 '16 at 17:25
  • I must be doing something wrong. I can't get your example to work. I have updated my question to show what I tried to do. – Patrick W. McMahon Jan 15 '16 at 18:13
  • I would like the base scope to be the current sites files directory – Patrick W. McMahon Jan 15 '16 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.