Jump to content

Download ID


dan09

Recommended Posts

create a database with 2 fields for sure:

id: Auto incremented unique id (a.k.a. 30 from your example)

file_location: the location of the file to download.

 

next you need need to make a page that gets that id and queries the database for the correct file.

next, to download the file just use the first example found here: readfile

 

That should get you started. When you have some questions post your code with your questions.

Link to comment
Share on other sites

<?php

// Make a MySQL Connection

mysql_connect("localhost", "username", "Password") or die(mysql_error());

mysql_select_db("database") or die(mysql_error());

 

// Retrieve all the data from the "example" table

$result = mysql_query("SELECT * FROM download")

or die(mysql_error());  

 

// store the record of the "example" table into $row

$row = mysql_fetch_array( $result );

// Print out the contents of the entry 

 

echo "Id: ".$row['id'];

echo " File: ".$row['file_location'];

 

?>

 

 

Ok i have this to query my database it works

Could you suggest any other moddifications where needed please?

 

The next thing is to use that example you suggested to me.

Im not sure how i would use it.

Could you help me with this please?

 

Link to comment
Share on other sites

first off, you query selects everything from the database, you want to just get one item using the passed in value

 

Your query should look like this:

$id = (int)$_GET['id'];
$result = mysql_query("SELECT * FROM download where id = $id")

 

Next you will add this to the end of this file:

 

$file = $row['file_location'];

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

Link to comment
Share on other sites

first off, you query selects everything from the database, you want to just get one item using the passed in value

 

Your query should look like this:

$id = (int)$_GET['id'];
$result = mysql_query("SELECT * FROM download where id = $id")

 

 

 

Could you show me where abouts this would go in the script?

Sorry im new to php and phpmyadmin

Link to comment
Share on other sites

Ok i have added all the script into the file and it looks like this:

 

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "username", "Password") or die(mysql_error());

mysql_select_db("databasr") or die(mysql_error());

 

// Retrieve all the data from the "example" table

$id = (int)$_GET['id'];

$result = mysql_query("SELECT * FROM download where id = $id")

or die(mysql_error());  

 

// store the record of the "example" table into $row

$row = mysql_fetch_array( $result );

// Print out the contents of the entry 

 

echo "Id: ".$row['id'];

echo " File: ".$row['file_location'];

 

$file = $row['file_location'];

 

if (file_exists($file)) {

    header('Content-Description: File Transfer');

    header('Content-Type: application/octet-stream');

    header('Content-Disposition: attachment; filename='.basename($file));

    header('Content-Transfer-Encoding: binary');

    header('Expires: 0');

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

    header('Pragma: public');

    header('Content-Length: ' . filesize($file));

    ob_clean();

    flush();

    readfile($file);

    exit;

}

 

?>

 

 

When i load the script (download.php) All that is displayed is: Id: File:

No data

Link to comment
Share on other sites

A: your url is wrong

B: Re-read my previous post

C: After fixing your code post it

 

A) the url is correct i have tested it out

B) i forgot to delete echo of file and id. Now nothing is displayed and no download stating.

C) still not fixed

Link to comment
Share on other sites

First off, you posted this as your url:

domain.com/download.php?=id1

 

Which is wrong, you want this:

domain.com/download.php?id=1

 

Second you have the wrong file location in your database, that is why the file is not downloading.

 

So if your file is located in a sub directory of the current directory (the one that contains download.php) and the directory is called "files"

 

you would have this as the value "files/image1.jpg" in your database.

Link to comment
Share on other sites

Sorry i meant to put :domain.com/download.php?id=1

 

On the database i have changed where the file is stored.

But still not working

 

Im not sure whey its not working

 

Here is my script:

 

 

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "username", "Password") or die(mysql_error());

mysql_select_db("database") or die(mysql_error());

 

// Retrieve all the data from the "example" table

$id = (int)$_GET['id'];

$result = mysql_query("SELECT * FROM download where id = $id")

or die(mysql_error());  

 

// store the record of the "example" table into $row

$row = mysql_fetch_array( $result );

// Print out the contents of the entry 

 

$file = $row['file_location'];

 

if (file_exists($file)) {  

  

    header('Content-Description: File Transfer');  

    header('Content-Type: application/octet-stream');  

    header('Content-Disposition: attachment; filename="file.zip"');  

    header('Content-Transfer-Encoding: binary');  

    header('Expires: 0');  

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  

    header('Pragma: public');  

    header('Content-Length: ' . filesize($file));  

  

    ob_clean();  

    flush();  

    readfile($file);  

    ob_clean();  

    flush();  

    exit;  

  

}

 

Link to comment
Share on other sites

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.