Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post

    File download help

    I am having problem with a file download script, i can upload the file fine but i can same to download it, when i select download the page appears blank. Can anyone help:

    my code is:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    if(isset(
    $_GET['id']))
    {
        include(
    "database.php");

        
    $id      $_GET['id'];
        
    $query   "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
        
    $result  mysql_query($query) or die('Error, query failed');
        list(
    $name$type$size$filePath) = mysql_fetch_array($result);

        
    header("Content-Disposition: attachment; filename=$name");
        
    header("Content-length: $size");
        
    header("Content-type: $type");
        
        
    readfile($filePath);

            
        exit;
    }

    ?>

    <html>
    <head>
    <title>Download CV</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    include("database.php");

    $query  "SELECT id, name FROM upload2";
    $result mysql_query($query) or die('Error, query failed');
    if(
    mysql_num_rows($result) == 0)
    {
        echo 
    "Database is empty <br>";

    else
    {
        while(list(
    $id$name) = mysql_fetch_array($result))
        {
    ?>
        <a href="download2.php?id=<?=$id;?>"><?=$name;?></a> <br>
    <?php        
        
    }
    }
    ?>
    </body>
    </html>

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    exit;
    i guess this makes the page blank!!

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    exit;
    i guess this makes the page blank!!
    Thanks for the reply, ive tried removing it but i still cant get the page to display anything, is there nethin else i can try?

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    have you got a link?

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    have you got a link?
    I have the file uploading to a folder on the server called "upload" and it is also stored in a database table, the name of file, size, type etc, that works fine. I just cant see anything once i run the download script to download and view the file.

    This is the upload code, if it helps any

    Code:
    <head>
    <title>Upload CV</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    <!--
    .box {
    	font-family: Arial, Helvetica, sans-serif;
    	font-size: 12px;
    	border: 1px solid #000000;
    }
    -->
    </style>
    </head>
    
    <body>
    <?php
    $uploadDir = 'C:/wamp/www/upload/';
    
    
    if(isset($_POST['upload']))
    {
    	$fileName = $_FILES['userfile']['name'];
    	$tmpName  = $_FILES['userfile']['tmp_name'];
    	$fileSize = $_FILES['userfile']['size'];
    	$fileType = $_FILES['userfile']['type'];
    
        // the files saved in filePath 
        $filePath = $uploadDir . $fileName;
    
        // move the files to the specified directory
    	
        $result    = move_uploaded_file($tmpName, $filePath);
    	if (!$result) {
    		echo "Error uploading file";
    		exit;
    	}
    
    	include("database.php");
    
        if(!get_magic_quotes_gpc())
        {
            $fileName  = addslashes($fileName);
            $filePath  = addslashes($filePath);
        }  
    
    	$query = "INSERT INTO upload2 (name, size, type, path ) ".
    			 "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
    
        mysql_query($query) or die('Error, query failed : ' . mysql_error());                    
        
        echo "<br>File uploaded<br>";
    }		
    ?>
    <form action="" method="post" enctype="multipart/form-data" name="uploadform">
      <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
        <tr> 
          <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
    		 </td>
          <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    Last edited by PRodgers4284; 02-15-2008 at 12:29 PM.

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    // force to download a file
    $file "http://localhost/test/".$_GET['file']."";

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    header("Content-Type: application/force-download");
    header"Content-Disposition: attachment; filename=".basename($file));

    header"Content-Description: File Transfer");
    @
    readfile($file); 
    found this code earlier today whilst browsing around, try it see if it works.

  • Users who have thanked rafiki for this post:

    PRodgers4284 (02-15-2008)

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    PHP Code:
    // force to download a file
    $file "http://localhost/test/".$_GET['file']."";

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    header("Content-Type: application/force-download");
    header"Content-Disposition: attachment; filename=".basename($file));

    header"Content-Description: File Transfer");
    @
    readfile($file); 
    found this code earlier today whilst browsing around, try it see if it works.
    Thanks for that rafiki

  • #8
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    did it work?

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Posts
    216
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    did it work?
    Got it sorted, thanks again for your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •