Hi,
I need to upload pdf file using php and store that pdf files in certain path.The pdf which is under Acrobat Distiller is uploading successfully.But the pdf files which is under AFPL Ghostscript
is not uploaded.

coding:

echo $_FILES['file']['type'];
if($_FILES['file']['type']=="application/pdf")
{
    if(file_exists("$filepath/upload/$basename"))
    {
        //file already exists
        return false;
    }
    else 
    {
        //move uploaded file    
move_uploaded_file($_FILES["file"]["tmp_name"], "$filepath/upload/$basename/" . 

$_FILES["file"]["name"]);
     }

.

pdf files information :successfuly uploaded pdf file information
Producer: Acrobat Distiller 5.0 (Windows)
CreationDate:   Sat Dec 12 17:38:54 2009
ModDate:        Sat Dec 12 17:38:54 2009
Tagged:         no
Pages:          1
Encrypted:      no
Page size:      765 x 992 pts
File size:      70313 bytes
Optimized:      no
PDF version:    1.4

.

pdf files information :Not  uploaded pdf file information
Producer:       AFPL Ghostscript 8.51
CreationDate:   Sun Dec 13 08:21:31 2009
ModDate:        Sun Dec 13 08:21:31 2009
Tagged:         no
Pages:          1
Encrypted:      no
Page size:      943.6 x 1512 pts
File size:      2417366 bytes
Optimized:      no
PDF version:    1.3

Thank you in advance,
prem

Recommended Answers

All 2 Replies

Hey.

I see you print the type of the file at the top of the script. Is it the same for both versions?

You could go one step further and just check the header of the files themselves. All valid PDF files should start with "%PDF", so you could just read that instead.

For example:

<?php
/**
 * Verifies that a file has a valid PDF header.
 * @param string $filepath The path to the file.
 * @return bool
 */
function is_pdf($filepath) {
    if(file_exists($filepath)) {
        $pdfh = fopen($filepath, "rb");
        if($pdfh) {
            $header = fread($pdfh, 4);
            fclose($pdfh);
            return $header == '%PDF';
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

/*
 * Main
 */
if(is_pdf($_FILES['file']['type'])) {
    // Do your file processing here.
}
else {
    echo "This is not a PDF file!";
}

?>

As an afterthought...

If you use something like this to validate the uploaded files, make sure you verify the file extension. Otherwise people could upload scripts directly to your server and execute them via a browser.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.