I have the following line in my script, but it fails to save the file in site/default/files folder. Is it possible I can replace public://feedproducts.xml with an actual absolute path to the files directory?

$file = file_save_data($sxe->asXML(),"public://feedproducts.xml",FILE_EXISTS_REPLACE);

This script runs from the command line, and is inside a sub-directory inside the root. It also loads Drupal Bootstrap.


No. Read the docs on file_save_data(), it needs a valid URI. otherwise it will not work.

If file_save_data() is returning false with the above code, you're better off investigating why. It's likely a permission or some other problem preventing file I/O on that directory.

  • You are correct; however, I haven't been able to pinpoint what the issue is. I can save data using file_put_contents but not file_save_date. The permissions and path both seem to be ok. Any chance this might have anything to do with me running php 5.6? – farjam Oct 5 '15 at 13:59
  • I'm not familiar with any odd behavior with PHP 5.6 & the Drupal file API. I'd look through watchdog (and Apache too if it's logging anything) for any telltale signs from Drupal/PHP. Failing that, follow the flow of execution in file_unmanaged_save_data(). Somewhere in there the save operation is failing. – Shawn Conn Oct 5 '15 at 16:37

All systems have a file stream wrapper, as far as I know, so try:

$uri = 'file:///absolute/path/to/file.ext';
file_save_data($sxe->asXml, $uri, FILE_EXISTS_REPLACE);

The issue was that the script was running from a subdirectory. Calling chdir(DRUPAL_ROOT) before file_save_data() fixed it.

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.