If we need to display a list of files so that we can access them via the link, it is recommended to display those links only if the files exist.
Thus, in the first phase, we define the path to the respective files. For example, let’s say it’s a folder with some pdfs. Because we need to get to the path on the HDD, not the URL, we will use the $_SERVER[‘DOCUMENT_ROOT’] variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php $cale="/fisiere/pdfuri/"; // calea pe server $sql = "SELECT * FROM fisiere"; $result = mysqli_query($con,$sql); ?> <table class="tabel"> <tr><th>Spec</th><th><th>Fisierul</th></tr> <?php WHILE ($rd = mysqli_fetch_array($result, MYSQLI_NUM)) { $fisier_exist=$_SERVER['DOCUMENT_ROOT'].$cale.$rd["2"].'.pdf'; if(file_exists($fisier_exist)) { $fisier_url='<a href="'.$cale.$rd["2"].'.pdf'.'" target="_blank"><i class="fa fa-file-pdf-o"></i> '.$rd["2"].'</a>'; } else { $fisier_url='-'; } ?> <tr> <td><?php echo $rd["1"] ?></td> <td><?php echo $fisier_url; ?></td> </tr> <?php } echo '</table>'; mysqli_free_result($result); mysqli_close($con); ?> |
As you can see, the link itself does not need the $_SERVER [‘DOCUMENT_ROOT’] variable.
Source: shorturl.at/mwFW7