Jump to content

php mssql file upload download


devofash

Recommended Posts

hey guys need some help. I'm trying to upload/download script which uploads pdf files into mssql server. i'm having some problems hope you guys can help me out.

 

i've been here http://www.phpfreaks.com/forums/index.php/topic,146750.0.html ...

 

the script works when I use txt and image files but when i upload pdf's I can't download it. I get an error saying "There was an error opening this document. The file is damaged or could not be repaired."

 

the datatype of the field is set to image

 

i'm posting my code below, please help me out.

 

UPLOAD.PHP

        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        $location = $_POST['location'];

        $datastring= file_get_contents($tmpName);
        
        // Unpacks from a binary string into an array according to the given
        $data=unpack("H*hex", $datastring);

	$fp = fopen($tmpName, 'r');
	$content = fread($fp, $fileSize);
	//$content = addslashes($content);
	fclose($fp);

        $db = new dbase();

        $db->db_connect();
        		
	$q = "INSERT INTO casestudies(location, filename, filecontents) ".
	     "VALUES ('$location', '$fileName', 0x".$data['hex'].");";

        $r = $db->query($q);

 

GETFILE.PHP

        $q = "select * from casestudies WHERE id='".$_GET['id']."'";
        $results = $db->query($q);
        $numRows = $db->numRows($results);

        if($numRows > 0) 
        {
        
            while($rows = mssql_fetch_object($results))
            {  
            
                header("Content-type: application/pdf");
                header("Content-Disposition: attachment; filename=$rows->filename");
                echo $rows->filecontents;
       }
        }
        else 
        {
            echo "No such file exists on Database";
        }

 

I would really appreciate some help . thanx

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.