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.
Related
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);
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");
?>
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
trying to figure out how i can make a and save a file while including default info from another file. i tried include but it did not work. any suggestion on how to build this file?
<?php
$wrtID = $_POST["fileID"];
SQL statement here to get relevant info
mkdir("CNC/$wrtID", 0770, true);
?>
<?php
$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
#ECHO OFF
#ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
#ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "
include("defaultcnc.php");
if ( additional file pats needed ) {
include("component-1.php");
}
";
fwrite($slfile, $txt);
fclose($slfile);
?>
I don't see a problem in the first block of code.
On the second block, the interpreter will consider the second double quotes to mean the end of the string $txt = " include(". So everything after that would produce a PHP error.
But even if you escape those the mpr file will have the string include("defaultcnc,php"); but not the actual contents of that file. For that you should do file_get_contents("defaultcnc.php").
something like:
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);
if ( additional file pats needed ) {
$txt .= file_get_contents("component-1.php");
}
fwrite($slfile, $txt);
fclose($slfile);
?>
I assume additional file pats needed means something to you. It should be a condition evaluating either true or false.
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)