The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End download – Generate Filename from Field (Array)?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • #177421

    I have a download script that generates a filename with a prefix followed by a random number to store a HTML page as a png file using html2canvas. Is it possible to generate the filename using the name field in a database? I have data for Tennis players and each players name is stored in the field ‘name (varchar(60)’ and use a fetch array query to loop through the records and store the fields as arrays e.g.

    while($row = mysql_fetch_array($result)){
    $tcgname[]= $row['name'];

    When a user clicks the download button I want the filename to be the player’s name. Is it possible to do this using the array that stores the name above? If so what change do I need to make to my download script? I have tried things like :

    header('Content-Disposition: attachment; filename=($tcgname[$i]).'.png');

    With $i being a count of the array (I have omitted the code).

    But get syntax errors.

    This is my download script :

    $file = trim($_GET['path']);
    // force user to download the image
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: image/png');
        header('Content-Disposition: attachment; filename=tcg_'.rand(100,999).'.png');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
    else {
        echo "error not found";
    Ilan Firsov

    In the example you have tried you have a missing single-quote'. either fix the concatenation (and missing quote), or change the single-quote to double-quotes and change the brackets to curly brackets {}
    either way should work:

        header('Content-Disposition: attachment; filename=' . $tcgname[$i] . '.png'); //the brackets are not required here


        header("Content-Disposition: attachment; filename={$tcgname[$i]}.png");

    Both pieces of code output a file named png i.e. not extension just png.


    Both pieces of code output a file named png i.e. not extension just png.

    Which would indicate that $tcgname[$i] is empty (or is not set). Do you have error reporting enabled? You can inspect the contents of $tcgname and $i using var_dump.


    $file = trim($_GET['path']);
    //  . . .

    You need to validate the contents of $_GET['path'] (e.g., by checking that it names a file in a particular “allowed” directory). Using this script, one may read —and then delete— any file on your computer that is readable by PHP, simply by knowing, or guessing, its name.


    Yes error reporting is enabled.


    Displays the value of the name field inside a table cell and all the values in this field can be seen by paginating through the records when displaying and editing records so there’s no issue there. If I add :

    var_dump ($tcgname[$i]);

    In the download.php script it gives a headers already modified/sent error.

    Ilan Firsov

    Headers already sent warning is perfectly fine here since you are changing headers after your var_dump output (and it is a warning not an error. it does not prevent the rest of the code from executing unless it relies on the new headers)

    What is the output of the var_dump? I’d try to var_dump $tcgname and $i as well (sometimes even the $row) just to make sure that they are set correctly, but that’s just me and my debugging techniqe, I like to dump everything that relates to my problem :)

    You might want to put an exit; after the var_dump to make sure you only output the var_dump


    If I add : var_dump ($tcgname[$i]); In the download.php script it gives a headers already modified/sent error.

    Which is to be expected, since we are interrupting the script to look at the value of that variable. But what is the output of var_dump?

    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
    After looking back over the code you have posted, I find myself more confused. Is this a single script, or two?

    The fact that you say you want something to happen “When a user clicks the download button” implies that you’re dealing with two page requests, as well. If this is the case, you do realize that variables from the first script/request don’t exist in the second…?

    Perhaps you should explain the situation here in a little more detail.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.