TRemmie Posted April 4, 2008 Share Posted April 4, 2008 This is a basic Php code from a PDF creator, and it prompts the person who generated the PDF to save it to their local computer, however I want it to send it to my server where I can access it to Print out the PDF. I am a huge php newbie but what on earth is making this prompt as 'SaveAs' and how on earth do I change it so it sends it to me? ??? [code=php:0]// add headers for download dialog-box header('Content-Type: application/pdf'); header('Content-Length: '.strlen($pdf)); header('Content-disposition:'.$method.'; filename="'.$name.'"'); echo $pdf; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/ Share on other sites More sharing options...
Catfish Posted April 5, 2008 Share Posted April 5, 2008 When you say "send it to my server" what do you mean? Is "your server" the computer the script is on or is it another computer seperate to the server the script is on and the client computer? From what I know about browser software, the code you showed is just sending the PDF data to the user through the browser and the client's browser will be checking what to do with the PDF file. If no program is set to handle the PDF file type it will just ask the user to save the file instead. That's my guess at it but maybe someone else here would know more. I haven't worked with PDFs before but I'd say you should look at PDF functions in the manual http://au.php.net/manual/en/ref.pdf.php and maybe the filesystem functions http://au.php.net/manual/en/ref.filesystem.php Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-509705 Share on other sites More sharing options...
TRemmie Posted April 8, 2008 Author Share Posted April 8, 2008 I want it to save to the same folder the Script is being run from. I was thinking it would look something like this but doesnt seem to work and I am quite the php newbie. $pdf = $_SERVER['DOCUMENT_ROOT']."/pdfproject/nameofpdf.pdf"; Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-512514 Share on other sites More sharing options...
TRemmie Posted April 9, 2008 Author Share Posted April 9, 2008 ??? can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-513099 Share on other sites More sharing options...
wildteen88 Posted April 9, 2008 Share Posted April 9, 2008 I assume the $pdf variable holds the data to be added to the .pdf file in which case you'd use file_put_contents function to create the .pdf file on your server. You wont need to use header to set the content type of the file. Example: // setup the path for the pdf file to be created $pdf_file_path = $_SERVER['DOCUMENT_ROOT'].'/pdfproject/'.$name; // check that the file doesn't exists first before creating it if(!file_exists($pdf_file_path)) { // file doesn't exists, so create the file. file_put_contents($pdf_file_path, $pdf); } Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-513185 Share on other sites More sharing options...
TRemmie Posted April 9, 2008 Author Share Posted April 9, 2008 Original: <?php $method = $_GET['method']; $name = $_GET['name']; if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) { // get bytearray $pdf = $GLOBALS["HTTP_RAW_POST_DATA"]; // add headers for download dialog-box header('Content-Type: application/pdf'); header('Content-Length: '.strlen($pdf)); header('Content-disposition:'.$method.'; filename="'.$name.'"'); echo $pdf; } else echo 'An error occured.'; ?> Edited: <?php $method = $_GET['method']; $name = $_GET['name']; if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) { // get bytearray $pdf = $GLOBALS["HTTP_RAW_POST_DATA"]; $pdf_file_path = $_SERVER['DOCUMENT_ROOT'].'/pdfproject/'.$name; // check that the file doesn't exists first before creating it if(!file_exists($pdf_file_path)) { // file doesn't exists, so create the file. file_put_contents($pdf_file_path, $pdf); } } else echo 'An error occured.'; ?> This isnt working, any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-513284 Share on other sites More sharing options...
wildteen88 Posted April 10, 2008 Share Posted April 10, 2008 What errors are you getting? Post any errors you're getting here. Your code maybe failing because I used a function called file_put_contents which is only available forn PHP5 or higher. Are you using PHP4? If you are you'll need to use fopen, fwrite and fclose instead. I have modified my code. You should see an error if the code still doesn't work: <?php // the following lines tell PHP to show an errors during runtime ini_set('display_errors', 1); error_reporting(E_ALL); if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] ) && isset($_GET['name'])) { // get bytearray $pdf = $GLOBALS["HTTP_RAW_POST_DATA"]; $path = $_SERVER['DOCUMENT_ROOT'].'/pdfproject/'; $pdf_name = $_GET['name']; // check that the /pdfproject folder is writable if(is_writable($path)) { echo '<p>"' . $path . '" is writable!<br />'; // check that the file doesn't exists first before creating it if(!file_exists($path.$pdf_name)) { // file doesn't exists, so create the file. echo '"'.$pdf_name.'" doesn\'t exist!<br />'; echo 'Creating PDF "' . $path.$pdf_name . '"... '; // create an empty file $hander = fopen($path.$pdf_name, 'w'); // attempt to write data to created file if (fwrite($handler, $pdf) === TRUE) { echo 'SUCCESS'; } else { echo 'FAIL'; } // close the file fclose($handler); } else { echo 'File already exists ('.$path.$pdf_name.')'; } } else { echo $path . 'is not writable! Unable to create PDF'; } } else { echo 'An error occured.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-514012 Share on other sites More sharing options...
TRemmie Posted April 11, 2008 Author Share Posted April 11, 2008 I got this error when running it on my Localhost /Library/WebServer/Documents/pdfproject/is not writable! Unable to create PDF So I put it on my webserver and got this, "/home/content/a/f/f/affimage/html/pdfproject/" is writable! "4/0 full bleed butterflycard" doesn't exist! Creating PDF "/home/content/a/f/f/affimage/html/pdfproject/4/0 full bleed butterflycard"... Warning: fopen(/home/content/a/f/f/affimage/html/pdfproject/4/0 full bleed butterflycard): failed to open stream: No such file or directory in /home/content/a/f/f/affimage/html/Create.php on line 28 Notice: Undefined variable: handler in /home/content/a/f/f/affimage/html/Create.php on line 31 Warning: fwrite(): supplied argument is not a valid stream resource in /home/content/a/f/f/affimage/html/Create.php on line 31 FAIL Notice: Undefined variable: handler in /home/content/a/f/f/affimage/html/Create.php on line 41 Warning: fclose(): supplied argument is not a valid stream resource in /home/content/a/f/f/affimage/html/Create.php on line 41 Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-514937 Share on other sites More sharing options...
TRemmie Posted April 11, 2008 Author Share Posted April 11, 2008 Ok scratch that I figured it out, however I was wondering. Is it possible to write in a code to like if the PDF is already there, overwrite it? Quote Link to comment https://forums.phpfreaks.com/topic/99614-changing-a-save-as-dialog-to-save-to-server-code-inside/#findComment-515081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.