سؤال

Alright, so I'm trying to accomplish the following:

  • generate a PDF with a filename based on Current Date + Current Time + variable from form called skift.
  • place said file in a folder that is static with the name skiftrapportPDF and place it in a subfolder that is named YearMonth examle(201404)

I've put togather a variable called $filename and when I echo $filename i get the following result.

/skiftrapportPDF/201404/Skiftrapport2014-04-16_125449-Orange.pdf

my code

        $dir = dirname(__FILE__);
        $skift = $_POST['skift'];
        $datum = Date("Y-m-d");
        $tid = Date("H:i:s");
        if($skift=='1')$skift='Svart';if($skift=='2')$skift='Blå';if($skift=='3')$skift='Röd';if($skift=='4')$skift='Orange';

        //this is when i have generated my $html
        require_once($dir.'/skift_rapport_pdf.php');

        require_once($dir.'/include/dompdf/dompdf_config.inc.php');  

        $dompdf = new DOMPDF(); // Create new instance of dompdf  
        $dompdf->load_html($html); // Load the html  
        $dompdf->render(); // Parse the html, convert to PDF  
        $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

        $month  = date('Ym');

        if (!is_dir("skiftrapportPDF/".$month)) {
          // dir doesn't exist, make it
          mkdir("skiftrapportPDF/".$month);
}
        $skift = $_POST['skift'];
        $datum = Date("Y-m-d");
        $tid = Date("His");
        if($skift=='1')$skift='Svart';if($skift=='2')$skift='Blå';if($skift=='3')$skift='Röd';if($skift=='4')$skift='Orange';
        $filename = '/skiftrapportPDF/'.$month.'/Skiftrapport'.$datum.'_'.$tid.'-'.$skift.'.pdf';

    //I've tried using either of the following....
    file_put_contents($filename, $pdf_content);
    //Commented to try using file_put_contents()
    //$fp = fopen($filename, "a"); 
    //fwrite($fp, $pdf_content); 
    //fclose($fp);

Using fopen(),fwrite,fclose gave me this error:

Warning: fopen(/skiftrapportPDF/201404/Skiftrapport2014-04-16_125449-Orange.pdf) [function.fopen]: failed to open stream: No such file or directory in file.php on line 863

and using file_put_contents() gave me this error.

Warning: file_put_contents(/skiftrapportPDF/201404/Skiftrapport2014-04-16_144631-Orange.pdf) [function.file-put-contents]: failed to open stream: No such file or directory in file.php on line 857
هل كانت مفيدة؟

المحلول

You just need to rewrite following line.

either use full server path or remove starting '/' from the file.

1) Use like this

$filename = $_SERVER['DOCUMENT_ROOT'] . '/skiftrapportPDF/'.$month.'/Skiftrapport'.$datum.'_'.$tid.'-'.$skift.'.pdf';

or write following way,

$filename = 'skiftrapportPDF/'.$month.'/Skiftrapport'.$datum.'_'.$tid.'-'.$skift.'.pdf';

these both will work for you. Just try any of them

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top