Connecting Tech Pros Worldwide Forums | Help | Site Map

Can I enable upload a pdf file with php ?

Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#1: May 15 '09
Hello,

Hopefully I can do this with php

I want to allow my clients to upload their pdf or video files to my server without giving them ftp login details.

Lets look at PDF's first:

This must be similar code to uploading an image - same thing really - just the checking must be different I guess?

This is what I have for the form:
I think that I have specified 10Mb and the max ( maybe that's a bit low ? )

Expand|Select|Wrap|Line Numbers
  1. /*
  2. *  upload_fm.php
  3. *
  4. */
  5. <form name="main_fm" enctype ="multipart/form-data" action ='upload_check.php' method = 'POST'>
  6. <input type = 'hidden' name='upld' value="on">
  7. <input type="hidden" name ="MAX FILE SIZE" value="10000000">
  8. <b>Your PDF:</b><br>
  9. <input type="file" size="50" id = "u1" name="upLoad">
  10. <input type="submit" value="Upload PDF">
  11. </form> 
  12.  
So far I have this to check the pdf but I have no idea how to proceed from here

I guess that I need to check somehow that it is a PDF and not some horrible virus program!

Then I need to move the file to a directory so that it can be access by another script to allow downloading.

Let's say that I want to put it on /MyPDF directory so the path will be
/MyPDF/nameofPDF.pdf

Would really appreciate any help

Expand|Select|Wrap|Line Numbers
  1. /*
  2. *   upload_check.php
  3. *
  4. */
  5.  
  6. $N_pdf  =    $_FILES['upLoad']["name"];
  7.  
  8.  
  9. // possible PHP upload errors 
  10. $errors = array(1 => 'php.ini max file size exceeded', 
  11.                 2 => 'html form max file size exceeded', 
  12.                 3 => 'file upload was only partial', 
  13.                 4 => 'no file was attached'); 
  14.  
  15. if ($_FILES['upLoad']['error'] != 0) { 
  16.         $message1 = "PDF file did not successfully upload" ; 
  17.         $message2 =  "Error: ".$errors[$_FILES['upLoad']['error']]; 
  18.         require_once ("upload_fm.php"); 
  19.         exit(); 
  20. }  // endif 

Thanks for any input

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: May 15 '09

re: Can I enable upload a pdf file with php ?


Hi.

Yes, the process of uploading a PDF is pretty much the same as uploading an Image.
The only difference is the validation process.

There are a few things you can check to see if it is in fact a PDF file, but there is never a way to be 100% sure.
The best defence against harmful files is to put them somewhere they can't be accessed by outsiders. (Outside the web-root)

Personally, to validate a PDF file, I would consider these steps sufficient:
  1. Make sure the mime type is "application/pdf"
  2. Make sure the file extension is .pdf
  3. Make sure the first 5 bytes of the file read "%PDF-" (As per the standard)
This should at least prevent accidental uploads of invalid files, and attempts to upload non-pdf files.

If all that checks out, the move_uploaded_file function should take care of the rest.
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#3: May 16 '09

re: Can I enable upload a pdf file with php ?


Thanks very much for your advice,
I think I can move forward on this now :)
Reply