Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422221
Share on other sites

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

  }
?>

Link to comment
https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-422273
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/83013-check-valid-pdf-file/#findComment-423319
Share on other sites

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.