Jump to content

Recommended Posts

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.

[code]
<?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;
}
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/21132-how-to-download-the-correct-linked-file/
Share on other sites

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
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...
Is the tracking field in the database unique?  You have this line:

[code=php:0]SELECT $filename,$filetype,$filesize,$filecontent FROM fileuploads WHERE tracking='$id'
[/code]

$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.

[color=red][size=8pt][b]EDIT:[/b][/size][/color] 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
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.