Jump to content


Photo

how to download the correct linked file?


  • Please log in to reply
3 replies to this topic

#1 snakez

snakez
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 07:52 AM

Guys i need your help again pls... Just refer the code below and this is my explanation: Firstly the user can upload as many files as he want, then that uploaded files will be saved to the database after submit button was clicked. After that, a form will appear with the lists of the files uploaded(displayed in filenames only with links on each of them). If the user clicks to a particular filename, the file associated with that filename will be downloaded by the user.

Ex. List of Files
1. First.doc
2. Second.txt
3. three.jpeg
4. four.gif

Now, base on the example above, if i click the first.doc, the first.doc will be downloaded. but when i click others, still the first.doc will be downloaded.

What should i do with my code below so that it will download the correct file that i clicked? By the way, the uploaded files was saved in an array.

<?php
 for($i=0;$i<count($_GET['id']);$i++){
 $id=$_GET['id'];
 $filename=$_GET['filename'];
 $filetype=$_GET['filetype'];
 $filesize=$_GET['filesize'];
 $filecontent=$_GET['filecontent'];
 if (isset($id)) {
        include "include/opendb.php";

        $query="SELECT $filename,$filetype,$filesize,$filecontent FROM fileuploads WHERE tracking='$id'";
        $result=mysql_query($query) or Die("Error locating file for tracking Number: ".$id." ".mysql_error());
        list($addfileName,$addfileType,$addfileSize,$addcontent)=mysql_fetch_array($result);

        header("Content-Disposition: attachment; filename=$addfileName");
        header("Content-length: $addfileSize");
        header("Content-type: $addfileType");
        echo $addcontent;

        exit;
 }
}
?>


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 September 2006 - 09:38 AM

Morning snakez.

The first thing you MUST confirm is that your database has the correct results in it.  If you look at the tables in the database, are the links correct or are they all pointing to the first file?

No point in checking the code on the page that pulls the info from the database if the database is incorrect (implying the code that inserts it is wrong).

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 snakez

snakez
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 September 2006 - 11:51 PM

I already checked the database and it works fine. The files are in correct situation in the table... By the way all of those files are having the same ID number("ex. 00001") and if a form will be viewed with an ID number for example "00001" then all of them will be displayed... And i already made them displayed in correct sequence using an array... perhaps the only problem here i think is how to linked them in a correct file or sequence...

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 September 2006 - 07:23 AM

Is the tracking field in the database unique?  You have this line:

SELECT $filename,$filetype,$filesize,$filecontent FROM fileuploads WHERE tracking='$id'

$id is coming from the variable $_GET['id'] and you're using WHERE with EQUALS instead of IN, so I was assuming there'd be only one id passed to the sql statement, but more than one row in the database with a 'tracking' value of $id.

EDIT: I've just had a thought.  In you're loop, you're outputting a header and then you echo content, then you loop again.  I'm not sure you can send headers more than once can you?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users