Jump to content

Recommended Posts

Hello,

 

I'm really stuck with the file upload. I submitted similar code a while ago in MSSQL section , but didnt get any reply at all so i've change the code but am still having problems. Some one please help.

 

I'm running PHP 4.4, IIS 5.1 and mssql

 

Ok, i'm uploading a PDF file to a directory, the upload works fine but when I download the file I get an eror "cannot open the file it may be corrupted". It really bugging me, I cannot figure out why its not working. the PDF files that I upload are fine but when I upload it I cant open them again. Please help.

 

upload.php

        include_once('../includes/dbclass.php');
        
        $uploadDir = "/upload/";

    	$fileName = $_FILES['userfile']['name'];
        
        $fileName = preg_replace("/[^a-z0-9-\.]/", "", strtolower($fileName));          
            	
    	$tmpName  = $_FILES['userfile']['tmp_name'];
    	$fileSize = $_FILES['userfile']['size'];
    	
    	$fileType = $_FILES['userfile']['type'];
    	
    	$title = escapeQuotes($_POST['title']);
    	$summary = escapeQuotes($_POST['summary']);
    	$location = $_POST['location'];

        $ext = substr($fileName, strrpos($fileName, '.') + 1);

        if (($ext != "pdf") && ($fileType != "application/pdf")) { header('Location: error.php'); }
        
        else 
        { 
            $filePath = $uploadDir . $fileName;
            
            
            $result = move_uploaded_file($tmpName, $filePath);
    	   
            if (!$result) 
            {
    		  header('Location: error.php');
    		  exit;
            }
    	
            require_once('../includes/dbclass.php');
        
            $db = new dbase();
        
            $db->db_connect();
            
             
            
            $q = "INSERT INTO casestudies (title, summary, filename, filesize, filepath, whichpage) ".
    	         "VALUES ('$title', '$summary', '$fileName', '$fileSize', '$filePath', '$location')";
    
            $results = $db->query($q);                    
    
            if($result) { header('Location: thankyou.php'); }
            else { header('Location: error.php'); }
            
            $db->db_close();
        }

 

 

download.php


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

            while($rows = mssql_fetch_object($results))
            {
                $filename = $rows->filename;
                                          
                //$find = array("%20", "_", "-", "'");
                //$replace = array(" ", "_", "-", "'");
                //$filename = str_replace($find, $replace, $filename);
                
                $filesize = $rows->filesize;
                $filepath = $rows->filepath;
                                             
                       
            header("Content-Disposition: attachment; filename=$filename");
            header("Content-length: $filesize");
            header("Content-type: application/pdf");
                header("Cache-control: must-revalidate");
                header("Content-Description: PHP Generated Data");

            readfile($filepath);


       }

 

and using this link to download relevent entries.

 

echo "<a href='download.php?id=$rows->id'>";

 

I will really really appreciate if someone could help me out. Thank you in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/68380-file-upload-problems-mssql-php-iis/
Share on other sites

  • 4 weeks later...
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.