- This topic is empty.
-
AuthorPosts
-
August 3, 2014 at 10:09 am #177421
MBM
ParticipantI 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'); <code></code>
With $i being a count of the array (I have omitted the code).
But get syntax errors.
This is my download script :
<?php $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)); ob_clean(); flush(); readfile($file); unlink($file); exit; } else { echo "error not found"; } ?>
August 4, 2014 at 2:34 am #177464Ilan Firsov
ParticipantIn 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
or
header("Content-Disposition: attachment; filename={$tcgname[$i]}.png");
August 4, 2014 at 7:22 am #177519MBM
ParticipantBoth pieces of code output a file named png i.e. not extension just png.
August 4, 2014 at 8:44 am #177527__
ParticipantBoth 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
usingvar_dump
.Also,
$file = trim($_GET['path']); // . . . readfile($file); unlink($file);
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.August 4, 2014 at 9:55 am #177539MBM
ParticipantYes error reporting is enabled.
$tcgname[$i]
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.
August 5, 2014 at 11:02 pm #177860Ilan Firsov
ParticipantHeaders 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 thevar_dump
to make sure you only output the var_dumpAugust 5, 2014 at 11:15 pm #177864__
ParticipantIf 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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.