Click here to Skip to main content
15,498,859 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">
    <link rel="stylesheet" href="style.css">
    <title>Files Upload and Download</title>
    <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>
    <th>size (in mb)</th>
  <?php foreach $files as $file; ?>
      <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>
  <?php endforeach;?>


// connect to the database
$conn = mysqli_connect('localhost', 'root', '', 'personal');
		die("Fatal Error: Connection Failed!");

	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);

What I have tried:

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

1 solution


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
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     CRLF
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)

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