Jump to content

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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251416
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251434
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251442
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251445
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251453
Share on other sites

delete or comment out:

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

 

if the file exists, it should start the download process, if it doesn't exist it won't do anything, so if nothing happens, you have the wrong path for your file.

 

you need to access the page like this:

http://yoursite.com/download.php?id=1

Link to comment
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251463
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251488
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
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251495
Share on other sites

it should look more like #1, but I don't know where your download.php file is, and where your files are located.

 

your #1 says that the file is located in the root of the computer (not the web server's public www) in a directory called files. is that the correct location?

Link to comment
https://forums.phpfreaks.com/topic/243724-download-id/#findComment-1251503
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.