jeeva Posted December 24, 2007 Share Posted December 24, 2007 hi frnds, i want validate the uploaded file is valid pdf file or not? Right now i have validated only by extension(.pdf) using javascript.And i also want to check the pdf content wether the content is valid pdf content or not? have u done anything like this? if s please let me know wt i have to do? Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/ Share on other sites More sharing options...
cooldude832 Posted December 24, 2007 Share Posted December 24, 2007 found this on php.net <?php define('PDF_MAGIC', "\\x25\\x50\\x44\\x46\\x2D"); function is_pdf($filename) { return (file_get_contents($filename, false, null, 0, strlen(PDF_MAGIC)) === PDF_MAGIC) ? true : false; } ?> So run that and if you get true the headers are pdf else it ain't Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422221 Share on other sites More sharing options...
jeeva Posted December 24, 2007 Author Share Posted December 24, 2007 thanks for reply cooldude832, The script is not working. Its returns false either way... Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422228 Share on other sites More sharing options...
mmarif4u Posted December 24, 2007 Share Posted December 24, 2007 Try this function: function GetExtention($type){ if($type == "application/pdf") return "pdf"; elseif($type == "application/x-pdf") return "pdf"; } Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422243 Share on other sites More sharing options...
jeeva Posted December 24, 2007 Author Share Posted December 24, 2007 sorry....no use.. Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422257 Share on other sites More sharing options...
Ken2k7 Posted December 24, 2007 Share Posted December 24, 2007 Can't you just use HTML? <INPUT TYPE="file" NAME="image" ACCEPT="pdf"> I think it's something like that. hmmm Or I think you can do this too: strpos($filename,'.pdf') Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422259 Share on other sites More sharing options...
mmarif4u Posted December 24, 2007 Share Posted December 24, 2007 Can you show us your complete code, how u use the function in it. Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422269 Share on other sites More sharing options...
jeeva Posted December 24, 2007 Author Share Posted December 24, 2007 Actually i have done with the extension part... i need to check the valid pdf file without testing the extension.In order to avoid the invalid pdf file i have to do this validation..... Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422271 Share on other sites More sharing options...
jeeva Posted December 24, 2007 Author Share Posted December 24, 2007 Here is my code <?php include_once("upload.inc.php"); if(isset($_POST['submit'])) { $myUploadobj = new UPLOAD; //creating instance of file. $upload_dir="images"; echo "<br>"; // use function to upload file. $file=$myUploadobj->upload_file($upload_dir,'file',true,true,0,"jpg|jpeg|gif|pdf"); if($file==false) echo $myUploadobj->error; else echo "File Name : ".$file; echo "<br>"; } ?> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style> a { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; color: #008080; text-decoration: none; font-size: 12px;} </style> </head> <body bgcolor="#FFFFFF" text="#000000"> <form name="test" method="POST" action="" enctype="multipart/form-data"> <input type="FILE" name="file"> <input type="SUBMIT" name="submit" value="submit"> </form> </body> </html> upload.inc.php <?php class UPLOAD { var $directory_name; var $max_filesize; var $error; var $file_name; var $full_name; var $file_size; var $file_type; var $check_file_type; var $thumb_name; var $tmp_name; function set_directory($dir_name = ".") { $this->directory_name = $dir_name; } function set_max_size($max_file = 3000000) { $this->max_filesize = $max_file; } function check_for_directory() { if (!file_exists($this->directory_name)) { mkdir($this->directory_name,0777); } @chmod($this->directory_name,0777); } function error() { return $this->error; } function set_file_size($file_size) { $this->file_size = $file_size; } function set_file_type($file_type) { $this->file_type = $file_type; } function get_file_type() { return $this->file_type; } function set_temp_name($temp_name) { $this->tmp_name = $temp_name; } function set_file_name($file) { $this->file_name = $file; $this->full_name = $this->directory_name."/".$file; } /* * @PARAMS : * $uploaddir : Directory Name in which uploaded file is placed * $name : file input type field name * $rename : you may pass string or boolean * true : rename the file if it already exists and returns the renamed file name. * String : rename the file to given string. * $replace =true : replace the file if it is already existing * $file_max_size : file size in bytes. 0 for default * $check_type : checks file type exp ."(jpg|gif|jpeg)" * * Example UPLOAD::upload_file("temp","file",true,true,0,"jpg|jpeg|bmp|gif") * * return : On success it will return file name else return (boolean)false */ function upload_file($uploaddir,$name,$rename=null,$replace=false,$file_max_size=0,$check_type="") { $this->set_file_type($_FILES[$name]['type']); $this->set_file_size($_FILES[$name]['size']); $this->error=$_FILES[$name]['error']; $this->set_temp_name($_FILES[$name]['tmp_name']); $this->set_max_size($file_max_size); $this->set_directory($uploaddir); $this->check_for_directory(); $this->set_file_name($_FILES[$name]['name']); if(!is_uploaded_file($this->tmp_name)) $this->error = "File ".$this->tmp_name." is not uploaded correctly."; if(empty($this->file_name)) $this->error = "File is not uploaded correctly."; if($this->error!="") return false; if(!empty($check_type)) { if(!eregi("\.($check_type)$",$this->file_name)) { $this->error="File type error : Not a valid file"; return false; } } if(!is_bool($rename)&&!empty($rename)) { if(preg_match("/\..*+$/",$this->file_name,$matches)) $this->set_file_name($rename.$matches[0]); } elseif($rename && file_exists($this->full_name)) { if(preg_match("/\..*+$/",$this->file_name,$matches)) $this->set_file_name(substr_replace($this->file_name,"_".rand(0, rand(0,99)),-strlen($matches[0]),0)); } if(file_exists($this->full_name)) { if($replace) @unlink($this->full_name); else { $this->error="File error : File already exists"; return false; } } $this->start_upload(); if($this->error!="") return false; else return $this->file_name; } function start_upload() { if(!isset($this->file_name)) $this->error = "You must define filename!"; if ($this->file_size <= 0) $this->error = "File size error (0): $this->file_size Bytes<br>"; if ($this->file_size > $this->max_filesize && $this->max_filesize!=0) $this->error = "File size error (1): $this->file_size Bytes<br>"; if ($this->error=="") { $destination=$this->full_name; if (!@move_uploaded_file ($this->tmp_name,$destination)) $this->error = "Impossible to copy ".$this->file_name." from $userfile to destination directory."; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422273 Share on other sites More sharing options...
jeeva Posted December 24, 2007 Author Share Posted December 24, 2007 nobody.........? Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422300 Share on other sites More sharing options...
~n[EO]n~ Posted December 24, 2007 Share Posted December 24, 2007 I do it like this <?php // $FILES file_pdf is name of the file field if(!ereg(".pdf", $_FILES["file_pdf"]["name"])) { echo "File should be of .pdf extension"; } else { echo "File is valid..."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422321 Share on other sites More sharing options...
jeeva Posted December 26, 2007 Author Share Posted December 26, 2007 i need to check the valid pdf file without testing the extension... Actually i have done with the extension part... Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-423271 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 I have this working on php 5.2.5: <?php define('PDF_MAGIC', "\x25\x50\x44\x46\x2D"); function is_pdf($filename) { return (file_get_contents($filename, false, null, 0, strlen(PDF_MAGIC)) === PDF_MAGIC) ? true : false; } Example: <?php $pdf_result = is_pdf('a_valid_pdf_file.pdf'); var_dump($pdf_result); // prints bool(true) $pdf_result = is_pdf('a_different_file.zip'); var_dump($pdf_result); // prints bool(false) $pdf_result = is_pdf('a_valid_pdf_file.zip'); var_dump($pdf_result); // prints bool(true) $pdf_result = is_pdf('a_different_file.pdf'); var_dump($pdf_result); // prints bool(false) So, regardless of file extension, the header is valid or not. Note that I just removed a leading slash from each value in the DEFINE. Slap this puppy solved if it works for you... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-423319 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.