I am working on a cron job that is creating a backup file. I want to move the file to the private file directory but I am having trouble figuring out how to do this. The cron job creates a file in a folder called /temp in the root of my site. I've been trying to use file_save_upload to move the file, but this isn't working

$gzip_file = $_SERVER['DOCUMENT_ROOT'] . '/temp/myfile.gz';
$private_file = 'private://myfolder/myfile.gz';
$drupal_file = file_save_upload($gzip_file,array(),$private_file,FILE_EXISTS_REPLACE);

I am not getting an error and $drupal_file equals false. The private folder exists and I can write to it with PHP. Is file_save_upload the wrong function? What do I need to do instead?


If it's an 'unmanaged' file, i.e one that doesn't have a database entry, you can use either

  1. file_unmanaged_move or
  2. file_unmanaged_save_data
| improve this answer | |
  • What of I want to make it a managed file? – Robbert Jan 9 '14 at 2:44
  • For managed files you can use file_save_data – David Thomas Jan 9 '14 at 8:20
  • Both are global functions... I can't believe there are no OO ways with services to do this. Any ideas? – ssibal Jul 20 '17 at 12:41

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.