Home > other >  How to call if file exists by ID
How to call if file exists by ID

Time:09-17

i have file in /pdf/0001_test.pdf
0001 is "id", and "test.pdf" is a file. how to call the "id" based on this "if file exists" code

<?php
$filename = "pdf/0001_test.pdf";
if (file_exists($filename)) {
    echo "File exist.";
} else {
    echo "File does not exist.";
}
?>

CodePudding user response:

<?php
  $file = "test.pdf";
  $filename = "pdf/".$d['id']."_".$file;
  if (file_exists($filename)) {
      echo "<p>".$fileName." exists.</p>";
  } else {
      echo "<p>".$fileName." not exists.</p>";
  }
?>

CodePudding user response:

this is my full code sir

<table border="1">
        <tr>
            <th>NO</th>
            <th>Nama</th>
            <th>NIM</th>
            <th>PDF FILE</th>
            <th>OPSI</th>
        </tr>
        <?php 
        include 'koneksi.php';
        $no = 1;
        $data = mysqli_query($koneksi,"select * from file");
        while($d = mysqli_fetch_array($data)){
            ?>
            <tr>
                <td><?php echo $no  ; ?></td>
                <td><?php echo $d['nama']; ?></td>
                <td><a href="pdf/<?php echo $d['pdf']; ?>" target="_blank">
                <b>
                    <?php
                        $filename = "pdf/0001_test.pdf";
                            if (file_exists($filename)) {
                                echo "File exist.";
                                } else {
                                echo "File does not exist.";
                                }
                    ?>
                </b>
                </td>
                <td><?php echo $d['alamat']; ?></td>
                <td>
                    <a href="edit.php?id=<?php echo $d['id']; ?>">EDIT</a>
                    <a href="hapus.php?id=<?php echo $d['id']; ?>">HAPUS</a>
                </td>
            </tr>
            <?php 
        }
        ?>
    </table>
  • Related