print generated file.txt using php / laravel - php

so i have this code for generate file.txt
<?php
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
then i show it on textarea like this:
<textarea rows="5" cols="50">
<?php
$file = file_get_contents('./test.txt', FILE_USE_INCLUDE_PATH);
echo $file;
?>
</textarea>
i need to print the file test.txt directly using button in php. How can i do this in php / laravel framework?? thanks

1 instead of showing the textarea, just show a link:
print text.txt!
2 create a new file called print_test_txt.php and populate it with this php
3
<?php
$file = file_get_contents('./test.txt', FILE_USE_INCLUDE_PATH);
echo $file;
?>
which will output it to the screen so the user can print it. if you are hoping to print to a known printer, see this solution:
directly print with php

Related

Session data to text file showing nothing

I have created something that once text is put inside of a text box and submit is pressed it sends this to a server and it is then stored in a session. On the press of another button you can retrieve this text and it is then transferred from the session to a txt file.
Yesterday I had this working and it was printing to my txt file but now all of a sudden it wont do it at all, can anyone spot any issues that would cause this?
This is ran when the retrieve button is pressed:
<?php session_start(); ?>
<?php
print_r($_SESSION ["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $_SESSION["input_data"];
fwrite($myfile, $txt);
fclose($myfile);
?>
I have narrowed it down to the $_SESSION["input_data"] part as it will print some random text in place for he session as seen above.
This is the creation of the session array:
<?php session_start(); ?>
<?php
$_SESSION["input_data"][] = $_POST["input"];
echo $_POST["input"];
?>
Many thanks to #marekful , I was tryign to print an array into a text file, I used implode() to fix this:
$strings = implode(" ", $_SESSION["input_data"]);
$myfile = fopen("TestingOut.txt", "a+") or die("Unable to open file!");
$txt = $strings;
fwrite($myfile, $txt);
fclose($myfile);

How to write a html file using PHP?

I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.

php code with random function not working

In this program i want that name of the image gets changed after it has been displayed on the page
But it gets changed before it could be displayed ...
the text file is used to get the name of image and also to store the new name of image file
<?php
$myfile = fopen("givename.txt", "r");
$d= fread($myfile,filesize("givename.txt"));
fclose($myfile);
img src="?php echo "80/$d.jpg";?>">
$rand=(mt_rand(10,100));
$mfile = fopen("givename.txt", "w") ;
fwrite($mfile, $rand);
fclose($mfile);
rename("80"."/"."$d".".jpg","80"."/"."$rand".".jpg");
?>
<?php
$myfile = fopen("givename.txt", "r");
$d= fread($myfile,filesize("givename.txt"));
fclose($myfile);
"<img src='80/$d.jpg'>";
$rand=(mt_rand(10,100));
$mfile = fopen("givename.txt", "w") ;
fwrite($mfile, $rand);
fclose($mfile);
rename("80"."/"."$d".".jpg","80"."/"."$rand".".jpg");
?>

Use fwrite to create an .md file

I was wondering if there is any way to use fwrite() to create an .md file. I am creating a blog for a team that needs to upload content without me typing it out. The content is in markdown via an .md file for each post. I am trying to make a web page that will take content and create an .md file with it. Right now I just have a proof test running and am trying to save this to a .md file. However, when I try to change the extension to .md I keep getting errors. I'm kinda a PHP noob so your help is apricated.
TLDR
I'm wondering why this (fwrite("newfile.md", "w") or die("Unable to create file!") would throw an error while using .txt won't.
CODE
<?php
$myfile = fwrite("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Your issue would be that you aren't opening/creating a file, you're just attempting to write to it. You need to use fopen() before you write to it:
$myfile = fopen("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);

Making new line in php write to file

I tried this but wont work
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
This is how it displays in my newfile.txt:
Mickey MouseMinnie Mouse`
but I want it like this:
Mickey Mouse
Minnie Mouse
It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character(s) for the platform PHP is running on.
$txt = "Goofy".PHP_EOL;
\r\n
add the carriage return in front.
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
file_put_contents($myFile,$txt ,FILE_APPEND);
fclose($myfile);
?>
if you want write \n in file use from file_put_contents($File_Handle,$Text_For_Write,FILE_APPEND)

Categories