0

Hello I'm doing my project and i want to ask just a simple question. Every time I go to that page it downloads automatically without getting inside on that page. When I click the button it automatically download the files. But I want to go that page and click a link before downloading it. Anyone help?

if(isset($_GET['profile']))
{
    $id=$_GET['profile'];
    $query = "SELECT * FROM profile_table WHERE id=?";
    $stmt=$con->prepare($query);
    $stmt->bind_param("i",$id);

    $rc = $stmt->execute();

    if ( false===$rc ) {
        die('execute() failed: ' . htmlspecialchars($stmt->error));
      }

    $result = $stmt->get_result();
    $row = $result->fetch_assoc();

    $vid = $row['id'];
    $vfname = $row['fname'];
    $vlname = $row['lname'];
    $vmname = $row['mname'];
    $vsex = $row['sex'];
    $vage = $row['age'];
    $vbday = $row['bday'];
    $vbloodtype = $row['bloodtype'];
    $vheight = $row['height'];
    $vweight = $row['weight'];
    $vreligion = $row['religion'];
    $vcolorhair = $row['colorhair'];
    $vdistmark = $row['distmark'];
    $vmobile = $row['mobile'];
    $vtin = $row['tin'];
    $vphealth = $row['phealth'];
    $vlegaldep = $row['legaldep'];
    $vaddress = $row['address'];
    $vtsize = $row['tsize'];
    $vheadgear = $row['headgear'];
    $vshoes = $row['shoes'];
    $vuniform = $row['uniform'];
    $vphoto = $row['photo'];

    $vsoi = $row['soi'];

    if (file_exists($vsoi)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($vsoi));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize('documents/' . $row['name']));
        readfile('documents/' . $row['name']);

    }

}

I think the problem their is the file_exist in vsoi. And I dont know what to do or to change in my code. I appreciate all the answers. TIA!

Dharman
  • 30,962
  • 25
  • 85
  • 135
Eys
  • 21
  • 4

1 Answers1

0

I think first you need to understand what your code is trying to do, e-g

if (file_exists($vsoi)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($vsoi));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize('documents/' . $row['name']));
    readfile('documents/' . $row['name']);

}

Above code is used to when we want to force a browser to download a file.

But in your case you only want to download the file when the page is loaded and user clicks on a download link and then the download starts.

It can be achieved by just displaying a download link which will then execute the same code you wrote in the if statement.

So change your if statement as following and then write the same code which was in the if statement previously to that download link page.

if (file_exists($vsoi)) {
    echo '<a href="'.$download_link.'">Download File</a>
}

You can simply display a direct download link to the file as following.

<php 
$download_link = 'https://yourdomain.com/files/sample.pdf';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1>Sample file</h1>
    <a href="<?php echo $download_link;?>" download="proposed_file_name">Download</a>
</body>
</html>
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • how can i write the code in the $download_link ? could you teach me or show me how to do it? thank you for answering my question! – Eys Aug 31 '19 at 10:51
  • @Eys There is nothing fancy to write code for download, it could be a simple link to the file that you can write directly in the anchor tag like i showed you above. I will edit my answer to clarify it even more. – Umer Abbas Aug 31 '19 at 11:10
  • I guess you got it wrong because I want a download link based on their ID. That's why i create a file exist and not a direct path to download it because I want to display them by ID. Can you help me further? – Eys Aug 31 '19 at 11:22
  • To help you to understand my question here is the pic of my database where the files located and need to download it. https://i.stack.imgur.com/d3ect.jpg – Eys Aug 31 '19 at 11:24
  • It's still the same process, You are getting profile Id from a get request (if(isset($_GET['profile']))), and then you are querying your result from db, that means you already have a file based on ID. so now what you want to do with it ? do you want them to only download it once ? make a unique link for that file ? help mem understand what you want to do with the file ? – Umer Abbas Aug 31 '19 at 11:36