File names stored in DB - Fetch corresponding files from flat file and display

Hellooo

I have some code that allows a user to upload documents (text, word, PDF). The documents ares stored in a folder directory and the names of the files are created before being inserted into a database column. The document names are displayed in a table format and when i click an individual file, the contents of the file is displayed. All good! But I would now like to have another button that when pressed will take ALL the names from DB column and fetch ALL the corresponding documents (the actual documents, not just the text names) form the directory and display them all in ONE document. So basically, I have a table with a column (file) storing document names and i want to retrieve the actual documents from the hard drive and display them in a single document.

The code for uploading the documents is below (this part is ok). I also have some other code I haven;t included.
[php]<?php
include_once("…/config/connectDB.php");

if (isset($_POST[‘buttonUpload’])) {

$file = rand(1000, 100000) . "-" . $_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$folder = "Master Folder/";

$new_file_name = strtolower($file);

$final_file = str_replace(' ', '-', $new_file_name);

if (move_uploaded_file($file_loc, $folder . $final_file)) {
    $sql = "INSERT INTO applications(file) VALUES('$final_file')";
    mysqli_query($link, $sql);
    ?>
    <script>
        alert('Successfully uploaded');
        window.location.href = 'application.php?success';
    </script>
    <?php
} else {
    ?>
    <script>
        alert('error while uploading file');
        window.location.href = 'application.php?fail';
    </script>
    <?php
}

}
?>[/php]

Displaying the table and file names in a table. Clicking a file name shows the content of that document only.
[php] $sql = “SELECT * FROM applications”;
$result_set = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result_set)) {
?>


<?php echo $row['internshipID'] ?>
<?php echo $row['studentID'] ?>
<?php echo $row['studentName'] ?>
<?php echo $row['faculty'] ?>
<?php echo $row['studyLevel'] ?>
<?php echo $row['file'] ?>
view file

<?php
}
?>[/php]

I was trying something like below to retrieve and display all the documents but was being shown only the names in the DB :stuck_out_tongue:
[php]
if (isset($_POST[‘getAllResumes’])) {
$sql = “SELECT file FROM applications”;
$result = mysqli_query($link, $sql);

    if (!($result)) {
        die(mysqli_error($link));
    } else {
        while ($row = mysqli_fetch_array($result)) {
            echo $row['file'];
        }
    }
}[/php]

In order to do that, you would need to create a new document. FPDF would be a possible option.

Thanks for the link I’m looking into it now.

Could I just ask, the code that I have above, the files are not being stored in the DB are they? They are being stored on the file system, and accessed through the file name which is being stored in the DB. My friend was saying I should use BLOB. Is there anything wrong with the way it is setup now?

It is usually* a bad idea to actually store files themselves in binary in the database. You can, but it complicates things with diminished return value. It is easy to corrupt the file, it is easy to get the mime type wrong making it where you can’t do anything with the file.

You can easily put the file behind your public directory and have a script that pulls the file from your file system to secure them. It’s also easier to move the file around when it is in the file system.

Sponsor our Newsletter | Privacy Policy | Terms of Service