2

I have to write a custom module extension, that saves entityform submission after hitting submit button to a PDF file in the private://pdf/<file_name> uri.

At first step it generates the PDF file to a temp directory via TCPDF and then I try to use file_save_data to save it to private directory and upload the file data to the db.

When I upload the file to the public directory, there are no problems, but when I set the uri to private://<pathname/filename> it sends me an error message:

"The data could not be saved, because the destination is invalid. More information is available in the system log".

In the system log:

"The data could not be saved because the destination private://pdf/employee_aontest_richie_2014-06-03_00_00_00_59.pdf is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper."

The code, that should save the whole thing is this:

$title = $node->title; // TCPDF doesn't let you have spaces or colons in your file name
$title_exploded = explode(' ', $title);
$title_imploded = implode('_', $title_exploded);
$imp_exploded = explode(':', $title_imploded);
$imp_exp_imploded = implode('_', $imp_exploded);
$real_title = drupal_strtolower($imp_exp_imploded);
//end of title generation
$save_path = 'private://pdf/'.$real_title.'.pdf';
$handle = fopen($full_path_tcpdf_output, 'r');
$file = file_save_data($handle, $save_path);
fclose($handle);
unlink($full_path_tcpdf_output);

I think the file saving procedure should work this way as it works if I save to public.

I appreciate any further advices.

3
  • 1
    Have you configured the private filesystem? Is in /admin/config/media/file-system. Also, check that the private://pdf directory is created.
    – sanzante
    Jun 27, 2014 at 9:21
  • Thanks! It worked well. Didn't know I had to configure the private directory, because I tought that it has a default directory.
    – Dude92
    Jun 27, 2014 at 10:27
  • Let's move this to an answer to reflect is answer (and is right)
    – sanzante
    Jun 27, 2014 at 10:53

2 Answers 2

1

Check two things:

  • The private flesystem is configured: look in /admin/config/media/file-system.

  • The destination directory (private://pdf) is created. The exact path of private:// depends on where have tou configured the private filesystem.

0

You can use file_save_upload() function of drupal to save pdf file ,i used this function in my site and saved in private:// , and pdf file is uploaded easily their, Code that i have used in my site:-

// In form put this code

    $form['uploadrecord'] = array(
        '#type'=>'file',               
        '#title'=>'Upload Pdf',   
        '#attributes'=>array('class'=>array('upload_file')), 
     );   

// at form submit put this code

$validators=array();

      $file_upload_record = file_save_upload('uploadrecord',$validators, 'private://pdf/'.$real_title.'.pdf');

and checkout the permissions of pdf directory too.may this code will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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