Recently I started PHP programming and I am currently working on a project where users can buy games digitally. So here's the problem I am facing at the moment, I created a library section for users where they can see their games that they bought and download them by clicking a button but whenever I clicked on the download button it will automatically download the file named download.php script file where inside is a bunch of error messages instead of the file I wanted. Below are my codes for the project.
Download.php(Download Script)
<?php
require_once("conn.php");
if(isset($_GET['id'])){
$id = $_GET['id'];
$sql="SELECT * FROM games WHERE game_downloadpath=$id";
$result = mysqli_query($con,$sql);
$file = mysqli_fetch_assoc($result);
$filepath = 'gamefiles/' . $file['name'];
if(file_exists($filepath)){
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Desposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma:public');
header('Content-Length:' . filesize('gamefiles/' .$file['name']));
readfile('gamefiles/' . $file['name']);
}
}
?>
<script type="text/javascript">
window.location.replace("library.php");
</script>
Library.php(Only Important codes will be shown)
<div class="block"></div>
<section class="library-section">
<div class="sidebar-div">
<input type="text" placeholder="Search"> <button type="button" name="button">
<i class="fas fa-search"></i>
</button>
</div>
<div class="game-div">
<h1>all games</h1>
<?php
include("conn.php");
$CID = $_SESSION['id'];
$search = isset($_POST['searchbox']) ? $_POST['searchbox'] : '' ;
//for search function
if ($search == NULL)
{
$result = mysqli_query($con,"Select * from games inner join purchase on purchase.purchase_game = games.game_ID inner join users on users.user_id = purchase.purchase_customer where games.game_status = 1 AND purchase.purchase_customer='$CID'");
while($row = mysqli_fetch_array($result))
{ ?>
<div class="games">
<img src="<?php echo $row['game_photopath'] ?>">
<div class="info-div">
<h2><?php echo $row['game_name'] ?> </h2>
<h4></h4>
<a href="download.php?id=<?php echo $row['game_downloadpath']?>"><button type="submit" name="">
<i class="fas fa-download"></i>
</button></a>
</div>
</div>
<?php }
} ?>
<div class="block">
</div>
</div>
</div>
<!-- <div class="individual-game-div">
</div>-->
</section>
<!-- </form> -->
<!-- /Main Content -->
Here is the code in the file that I mentioned earlier.
<br />
<b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in <b>C:\xampp\htdocs\SDP-Nexus-master\Foong's part\download.php</b> on line <b>28</b><br />
<br />
<b>Notice</b>: Trying to access array offset on value of type null in <b>C:\xampp\htdocs\SDP-Nexus-master\Foong's part\download.php</b> on line <b>29</b><br />
<br />
<b>Notice</b>: Trying to access array offset on value of type null in <b>C:\xampp\htdocs\SDP-Nexus-master\Foong's part\download.php</b> on line <b>39</b><br />
<br />
<b>Notice</b>: Trying to access array offset on value of type null in <b>C:\xampp\htdocs\SDP-Nexus-master\Foong's part\download.php</b> on line <b>40</b><br />
<br />
<b>Warning</b>: readfile(gamefiles/): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\SDP-Nexus-master\Foong's part\download.php</b> on line <b>40</b><br />
<script type="text/javascript">
window.location.replace("library.php");
</script>