Jump to content

Changing a 'save as' dialog to save to server? [Code Inside]


Recommended Posts

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]

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

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);
}

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?

 

 

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.';
}

?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.