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

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.