0

i'm doing the download file in php.. i've shown all the file in a page and download when user click the download button.. the file name has been stored in an array session within a loop.. when the download button is click, new page downloadfiles.php is open and download the file.. problem is i can only pass the session array value manually.. how can i pass it according to user click on the button?

currently i using $file = $_SESSION['imagefilename'][0]; to manually download the first file in the database.. how can i automatically assign the [0] value based on user click?

display.php

<?php 
    //pass session email here
    $email = $_SESSION['email'];
    $image = mysql_query("SELECT * FROM image WHERE email='$email' ");
    $numrows = mysql_num_rows($image);
    if($numrows!=0)
    {
        $_SESSION['imagefilename'] = array();

    while ($row = mysql_fetch_assoc($image))
    {
        //get the image path
        $path = $row['path'];
        //get the image name
        $name = $row['name'];

        $_SESSION['imagefilename'][] = $name;
        $_SESSION['imgpath'] = $path;

            echo "<br>Your image: '$name' <br><img src='$path' width='300' height='200'><br><a href='/images/downloadfiles.php' data-role='button' data-theme='e' target='_self' name='download'>Download</a><br><a href='deleteimg.php' data-role='button' data-theme='e' target='_self'>Delete</a></br>";


    }
    }else
        echo "No image found!";

    ?>

images/downloadfiles.php

<?php
session_start();
    // Define the path to file

    $file = $_SESSION['imagefilename'][0];


   if(!$file)
   {
   // File doesn't exist, output error

   die('File not found');
   }
   else
   {
 // Set headers
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$file");
 header("Content-Type: application/force-download");
 header("Content-Transfer-Encoding: binary");


 // Read the file from disk
 readfile($file);
 }



 ?>

i'm doing the download file in php.. i've shown all the file in a page and download when user click the download button.. the file name has been stored in an array session within a loop.. when the download button is click, new page downloadfiles.php is open and download the file.. problem is i can only pass the session array value manually.. how can i pass it according to user click on the button?

currently i using $file = $_SESSION['imagefilename'][0]; to manually download the first file in the database.. how can i automatically assign the [0] value based on user click?

2 Answers 2

1

You need to embed the file's ID into the download link url:

echo "<br>Your[..snip..]<a href='/images/downloadfiles.php?id=$idx' etc...
                                                          ^^^^^^^^

and then retrieve it in the download script with:

session_start();

$id = $_GET['id'];

if (!isset($_SESSION['imagefilename'][$id])) {
    die("Invalid file id");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Warning: Hacker Friendly Code Detected :P
0

Why do want to use sessions ? Just send the image id as a get parameter and check the email after that.

display.php (in your while fetch_assoc)

<a href='/images/downloadfiles.php?id=<?php echo $row['id'];?>' data-role='button' data-theme='e' target='_self' name='download'>Download</a>

downloadfiles.php

 $id = isset($_GET['id']) ? $_GET['id'] : null;
 $image = mysql_query('SELECT * FROM image WHERE id="' . $id . '"');
 $numrows = mysql_num_rows($image);
 if($numrows!=0)
 {
   $row = mysql_fetch_assoc($image);
   if ($row['email'] == $_SESSION['email'])
   {
      // Header and readfile
   }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.