FPDF - output dynamic file name - php

I'm creating a PDF page from an html form, so my php looks like this (for example):
$pdf->Cell(0,0,$_POST['teacher_name'],0,2,'C');
What is my missing step that I can define that variable (teacher_name) to create my output file as:
$pdf->Output('homework-teacher_name.pdf', 'D');
and the teacher_name in the output is replaced by whatever is submitted in the form?

$fileName = 'homework-' . $_POST['teacher_name'] . '.pdf';
$pdf->Output($fileName, 'D');
Remember to sanitize external inputs using filter_input

nb. order of Output() parameters! according to http://www.fpdf.org/en/doc/output.htm ist should be:
$pdf->Output('D', $fileName);

$fullname = $row["fullname"];
$pdf_file_name = $fullname.".pdf";
$pdf->Output($pdf_file_name,'D');
It is working.

Related

PHP creating folder with two variables gives wrong foldername

I'm trying to create a random foldername for upload of files.
The folderpath shall be like "receivedfiles/$name$date/". Instead i get this: "receivedfiles/ . 13.06.2016/". I really don't know what is wrong...
The variable $name is user input and can be e.g. "Simon" or "Simon Jensen" depending on what the user wants.
$d = date('d.m.y');
$varfoldername = "../receivedfiles/. $name . $d ./";
mkdir($varfoldername , 0777 , true);
$upload_folder = $varfoldername;
Thanks to great help i have removed the dots from code above, please see code below which only name the folder with the date.
$name = $_POST['name'];
$d = date('d.m.y');
$varfoldername = "../receivedfiles/$name$d/";
mkdir($varfoldername , 0777 , true);
$upload_folder = $varfoldername;
Solution: $name was first specified later in script. Moving that atop solved part of the problem. Changed folder path as above solved the rest - thank you all :)
. The problem was that this is code from send email form. The email is composed below and fileupload is above. So of course the variable wasn't know at that line. 
Extraordinarily, you've written the solution in the question
$varfoldername = "../receivedfiles/$name$date/";
since it's double-quoted php interprets $.. as variable and print its content
by the way, print $varfoldername to see what is the output
Well, the extra dots are there because you are not concatenating correctly. The 2nd line should be something like
$varfoldername = "../receivedfiles/" . $name . $d . "/";
or you could remove the dots inside your string since you are using double quotes for your folder name value.
First of all, you need to check whether the $name variable is not empty.
Then, use sprintf for creating the path. It provides more flexible formatting and helps you to get rid of redundant spaces/dots/etc. Try this:
$varfoldername = sprintf('../receivedfiles/%s%s/', $name, $d);

PHP $filename with text

Quick question (hopefully)
I am saving a form as a pdf using php and would like to save the $filename as something like:
/New_Form_[user].pdf
(where [user] is $POST['forename']+$POST['surname'])
Is this possible? if so how?
my current code:
$filename = "HTC_619 New User.pdf";
The "New User" bit i want auto populated from Firstname and Surname from the form.
so:
$filename = "HTC_619_$POST['Forename']_$POST['Surname']
Thanks
Phil
yes you can do it like
header("Content-Disposition: attachment; filename=\"$filename\"")
$filename = "HTC_619". $username."_".$surname.".pdf";
pass the post first name and last name in $filename variable
$name=$_POST['forename']+$_POST['surname'];
$filename="htc_619"."_".$name.".pdf";

How can I add variable names to the end of a file?

I have created a form that submits the data to a filename on the server. The form submit is working fine, it generates the requested file called "we_input_.sts".
I am trying to use the following code to grab two variables from the form "bfstnme" and "gfstnme"and attach them to the filename eg "wed_import-Jane_Bill.sts
This is the amended code: However I am still unable to get it to work.
I am trying different ideas to get this to work correctly. I have tried moving the code around but I'm still obviously missing something. The last line before the $savestring== is "$fp=fopen("wed-import-.sts", "a+");
The last lines after the $savestring are : fwrite($fp,$savestring); fclose($fp);
<?php
$bfirstname = $_POST['bfstnme'];
$gfirstname = $_POST['gfstnme'];
$file = 'wed_import_.sts';
$current = file_get_contents($file);
$new_file = 'wed_input_'.$bfirstname.'&'.$gfirstname.'.sts';
file_put_contents($new_file, $current);
?>
Here is the way I have solved it using the valuable assistance of all concerned.
$names .= ("$bfstnme" . "$gfstnme");
$fp = fopen("wed_import_($names).sts", "a+");
The results of the above give me a filename called:
"wed_Import_[JaneBill].sts. I only need to work out how to put an amperstand (&) betwen the names.
Thank you to all.
If you want put the info inside the file you must change the + by a . like this:
$current .= ("gfirstname" . "bfirstname");
If you want change the name, you must do something like #Jay_P says
Why you don't name the file before writing to it?
<?php
$gfirstname = $_POST['gname'];
$bfirstname = $_POST['bname'];
$file = 'wed_input_Bride_Groom.sts';
// Opens the file to get existing content hopefully
$current = file_get_contents($file);
// Appends bride and groom first names to the file hopefully
$current .= ("gfirstname" . "bfirstname");
$new_file = 'wed_input_'.$gfirstname.'_'.$bfirstname.'.sts';
// Write the contents back to the file
file_put_contents($new_file, $current);
?>
Let's assume you have the names in a variable called $names. You can easily append the text with the FILE_APPEND flag like this:
file_put_contents('wed_input_Bride_Groom.sts', $names, FILE_APPEND);

php Update filename from directory

so the title is not full clear, my question , I'm using the code to rename the file from directory present in the server the problem is i have to use the HTML form and php to update the file name, i want to do this : there will be an option on every file for renaming it when i click on the option the box pops up and i have to type the new name for file and save it , any help will be appreciated. (before down voting think about the question.)
The code that I'm using to update the file name
<?php
include("configuration.php");
$target = $_POST['filename'];
$newName = $_POST['newfilename'];
$actfoler = $_REQUEST['folder'];
$file = "files/users/";
$new ="files/users/";
$renameResult = rename($file, $new);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $file . " is now named " . $new;
} else {
echo "Could not rename that file";
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Try changing these lines:
$file = "uploads/$loggedInUser->username$actfolder/$target";
$new ="uploads/$loggedInUser->username$actfolder/$newName";
To:
$file = "uploads/{$loggedInUser->username}{$actfolder}/{$target}";
$new ="uploads/{$loggedInUser->username}{$actfolder}/{$newName}";
To explain why:
You are using variables inside a string, which means you will want to tell PHP where the variable ends. Especially when referencing objects or arrays, but also when you are placing variables right next to each other. I'm guessing PHP evaluated your original line to uploads/[Object]->usernamePizza/newname
I don't think you can call object properties in a string as you do.
try replace these lines :
$file = "uploads/".$loggedInUser->username."$actfolder/$target";
$new ="uploads/".$loggedInUser->username."$actfolder/$newName";
You may think about echoing $file and $new to confirm the path is nicely built.
On a side note, I'd recommend to really check the entries because this code can obviously lead to major security issues.

Counting sent files

I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.

Categories