Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am trying to run code for download but i keep getting this problem,below are my codes
index php
<?php include 'filesLogic.php';?>
<!DOCTYPE html>

<html lang="en">
  <head>
    <link rel="stylesheet" href="style.css">
    <title>Files Upload and Download</title>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <form action="index.php" method="post" enctype="multipart/form-data" >
          <h3>Upload File</h3>
          <input type="file" name="myfile"> <br>
          <button type="submit" name="save">upload</button>
        </form>
      </div>
    </div>
	<table>
<thead>
    <th>ID</th>
    <th>Filename</th>
    <th>size (in mb)</th>
    <th>Downloads</th>
    <th>Action</th>
</thead>
<tbody>
  <?php foreach $files as $file; ?>
    <tr>
      <td><?php echo $file['id']; ?></td>
      <td><?php echo $file['name']; ?></td>
      <td><?php echo floor($file['size'] / 1000) . ' KB'; ?></td>
      <td><?php echo $file['downloads']; ?></td>
      <td><a href="downloads.php?file_id=<?php echo $file['id'] ?>">Download</a></td>
    </tr>
  <?php endforeach;?>

</tbody>
</table>
  </body>
</html>


filesLogic.php
<?php
// connect to the database
$conn = mysqli_connect('localhost', 'root', '', 'personal');
if(!$conn){
		die("Fatal Error: Connection Failed!");
	}
?>


downloads.php
<?php
	include 'filesLogic.php';
 	// Downloads files
if (isset($_GET['file_id'])) {
    $id = $_GET['file_id'];

    // fetch file to download from database
    $sql = "SELECT * FROM try WHERE id=$id";
    $result = mysqli_query($conn, $sql);

    $file = mysqli_fetch_assoc($result);
    $filepath = 'uploads/' . $file['name'];

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

        // Now update downloads count
        $newCount = $file['downloads'] + 1;
        $updateQuery = "UPDATE try SET downloads=$newCount WHERE id=$id";
        mysqli_query($conn, $updateQuery);
        exit;
    }
}
?>


What I have tried:

have tried to paste the index php into another code but i keep getting the same problem
Posted
Updated 8-Feb-21 14:49pm

1 solution

Hi,

When a compiler tells you it got an unexpected something, that really means right before that something something is lacking.

As the documentation[^] clearly shows, in PHP foreach requires parentheses.

You probably need to pay more attention to documentation or examples, the next likely problem you'll have is endforeach does not exist as a keyword. You're too creative!

:)
 
Share this answer
 
v2
Comments
Member 15067930 9-Feb-21 12:07pm    
can you please re-write how the code is suppose to be,am new in php,i will appreaciate.
Luc Pattyn 9-Feb-21 12:19pm    
You can't loop HTML code like that, put all the loop stuff inside PHP, like so (using period to concatenate strings, and semicolon to terminate PHP instructions):

<?PHP
foreach ($files as $file) {
echo "<tr>";
echo "<td>".$file['id']."</td>";
echo "<td>".$file['name']."</td>";
...
echo "</tr>";

}
?>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900