menu

Questions & Answers

How to download files from mysqli to web server directory?

I have a database on a web server, it has binary files stored in it, I need to download these files to the server directory, then change from bin to a PHP extension to capture data that appears in character blocks, I can do the whole process manually but I need to automate it, I don't know if it is possible to download files from the DB to the server's directory, or change its extension using PHP, I want to avoid discussions about the binary file, the focus is on these two questions if it is possible or not using PHP.

I know that there is this code to download from the server directory to the client, but I can't find a download from the DB to the server directory.

<?php 

if(!empty($_GET['file'])){

    $filename = basename($_GET['file']);
    $filepath = 'downloads/' . $filename;

    if(!empty($filename) && file_exists($filepath)){

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/zip");
        header("Content-Transfer-Emcoding: binary");

        readfile($filepath);

        exit;

    }

    else {

        echo "This File Does not exist.";

    }

}
Comments:
2023-01-22 23:01:12
Why do you have files store in your database in the first place?? Just run a normal sql query, open a file, put some part of the query returned fields into the opened file.
2023-01-22 23:01:12
Are they logging data from a game, is it modifying a tibia, so binary files are also opened?
2023-01-22 23:01:12
mysqli is a PHP extension. You can't "download" anything from it. Please clarify what you mean.
2023-01-22 23:01:12
anything is possible, though abit unclear, do a select, loop over result place the contents of the column in a file, like with file_put_contents it can be named anything you like, pref the original extension type and filename
2023-01-22 23:01:12
Muito obrigado! Lawrence Cherone
Answers(0) :