Loading ...
Sorry, an error occurred while loading the content.
 

downloading files using PHP

Expand Messages
  • Ali Abu-Ghosh
    Hi I am currently working on a project that uses PHP, MySQL and Apache I am doing an online submission system for assignments. The code that I implemented
    Message 1 of 1 , Jul 12, 2007
      Hi

      I am currently working on a project that uses PHP, MySQL and Apache

      I am doing an online submission system for assignments.

      The code that I implemented works fine and I am able to upload documents to
      my website and download them again.

      The problem is when I download the document , it becomes corrupted( i.e.: it
      contains rubbish data and symbols)

      I was wondering if there is any configuration setting that I should apply
      for Apache or PHP to prevent corrupting the documents when they get
      downloaded

      Here is the code that i use

      <html>
      <?php
      require 'studentsession.php';

      header('Cache-Control: public');


      if(isset($_GET['id']))
      {


      mysql_connect("localhost", "ali", "aliag") or die(mysql_error());
      mysql_select_db("assess") or die(mysql_error());

      $id = $_GET['id'];

      $query = "SELECT name, type, size, content FROM user_submission WHERE
      submit_id = '$id'";
      $result = mysql_query($query) or die('Error, query failed');
      list($name, $type, $size, $content) = mysql_fetch_array($result);



      header ("Content-type: $type");
      header ("Content-length: $size");
      header("Content-Transfer-Encoding: binary\n");
      header ("Content-Disposition: attachment; filename=$name");

      header ("Content-Description: PHP Generated Data");




      echo $content;
      exit;
      }

      ?>
      <html>
      <head>

      <title>Download File From MySQL</title>

      </head>

      <body bgcolor="#166961">
      <h2 align='center'> Individual Assignments</h2>
      <?php

      mysql_connect("localhost", "ali", "aliag") or die(mysql_error());
      mysql_select_db("assess") or die(mysql_error());

      $query = "SELECT submit_id,name,course_id FROM user_submission where
      user_id='$userid' ";
      $result = mysql_query($query) or die('Error, query failed');
      if(mysql_num_rows($result) == 0)
      {
      echo "Database is empty <br>";
      }
      else


      {

      echo"<p align='center'><table border=1>";
      $count=1;
      echo "<tr><th>Number</th><th>Course Name</th><th>Assignment</th>";
      while(list($id, $name,$courseid) = mysql_fetch_array($result))

      {

      $query2 = "SELECT course_name FROM courses where course_id='$courseid'";

      $result2 = mysql_query($query2) or die('Error, query failed');
      list($coursename)=mysql_fetch_array($result2);

      echo "<tr><td>$count</td><td>$coursename</td><td><a
      href='download.php?id=$id'>$name</a></td>";



      ++$count;

      }

      echo"</p></table>";

      }

      ?>
      </body>
      </html>



      Best Regards,

      Ali


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.