First goal: Create an index.html file and create a link to download the generated file
The issue here is when i click to generate new file the downloaded file is always the same and isnt updated
the variable $content has insite an entire html page with <headers><aside> and <sections>
I have the following code
if( empty( $error )){
echo "<h3>File generated</h3>";
$my_file = 'index.html';
if (file_exists($my_file)) {
if(unlink($my_file)){
};
$new_file = 'index.html';
$handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file);
$data = $content;
fwrite($handle, $data);
fclose($handle);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
} else {
$new_file = 'index.html';
$handle = fopen($new_file, 'w') or die('Cannot open file: '.$new_file);
$data = $content;
fwrite($handle, $data);
fclose($handle);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
}
hi i think it's will be work if variable $content will be contain correct data and you have permission to read, update, create file. in code I use file_put_contents function for minimize your code
if( empty( $error )){
echo "<h3>File generated</h3>";
$my_file = 'index.html';
file_put_contents($my_file, $content);
echo "<a download='index.html' href='index.html'><b class='download'>Download</b></a>";
}
UPDATE:
Are you sure that you have correctly generated content and the $content always contains different value?
Related
Hi I am trying to get this external text file to print inside my php document. The code looks fine to me however when I echo it does not output anything and I am not sure why this is. Can anybody help me out as I am new to this.
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
$fp = fopen($location, 'r');
if ($fp) {
$readin = fread($fp);
fclose($fp);
} else {
echo 'Can\'t open input.txt';
}
Not sure what you're trying to 'echo' but have you checked if the file exists in the first place?
Your code could be written as:
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
if (file_exists($location) && $data = file_get_content($location)){
echo $data;
} else {
echo 'File not found';
}
if (file_exists($location) && $file = fopen($location, 'r')){
$file_content = fread($file, filesize($location));
fclose($file);
} esle {
echo 'File not found';
}
See here for more: http://php.net/manual/en/function.file-get-contents.php, http://php.net/manual/en/function.filesize.php
I am new to PHP and I am currently working on File Handling. I have a text file of which I am attempting to open for reading/appending using a skeleton script. The file is outputting and showing it is successfully opening, but only when I add a include function into the code. I have my code below, can someone look at it and tell me if I am doing it right because it feels right to me at the minute and it does output but i'm not 100% positive.
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
include($location);
if (file_exists($location) && $file = fopen($location, 'r')){
$file_content = fread($file, filesize($location));
fclose($file);
} else {
echo 'File not found';
}
change your code to read and output file to below:
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
//include($location); remove include
if (file_exists($location) && $file = fopen($location, 'r')){
$file_content = fread($file, filesize($location));
echo $file_content; //<----echo here to display content
fclose($file);
} else {
echo 'File not found';
}
Another option is to use file_get_contents().
It will also read the text file but it will read the text files full contents to a string.
$location = '/Applications/MAMP/htdocs/PHPLabs/branches.txt';
if (file_exists($location)){
$file_content = file_get_contents($file);
Echo $file_content;
$file_content .= " And some more"; //append string to end of string
Echo $file_content; // echo with appended string.
File_put_contetnts($file, $file_content); // save the original text plus the appended.
} else {
echo 'File not found';
}
I have a text file :
Topic identified: Sports
I have 2 php files:
file 1:
<?php
include "topic_detection.php";
$text = isset($_POST["text"]) ? $_POST["text"] : '';
$file = "textfile.txt";
$outputfile= "outputfile.txt";
if(!empty($_POST)){
writetofile($file, $text, "w");
execpython($file);
$topic = getoutput($outputfile);
}
?>
file 2: topic_detection.php
<?php
function writetofile($file, $content, $writeType){
$fo = fopen($file, $writeType);
if($fo){
fwrite($fo,$content);
fclose($fo);
}
}
function execpython($file){
system("python predict.py $file");
}
function getoutput($file1){
$fh= fopen($file1, 'r');
$theData = fread($fh,1);
fclose($fh);
echo $theData;
return $theData;
}
?>
On trying to get the output of $theData, the only output I receive is a 'T' I am guessing this T is coming from the first letter in the text file. In that case, am I not making the call correctly?
here is my call:
<div align="center"><h4 style="line-height:150%;"><?php echo $topic; ?></h5></div>
You are passing fread a length of 1:
$theData = fread($fh,1);
You will always get only the first character.
To read the entire file: (From the PHP docs http://php.net/manual/en/function.fread.php)
$contents = fread($handle, filesize($filename));
You could also use http://php.net/manual/en/function.file-get-contents.php
I'm working on what seems to be a simple php reading and writing to a txt file. I'm able to read the file but not write. I've tried variations of the following but to no avail. I'm trying to read the txt file that will have a 0 or a 1 in it and switch it. But for some reason I can only read. How can I make this code write to the file? Note I do have write permissions to the txt file.
Thx
Rich
<?php
$status = readfile("0.txt");
echo $status;
$file_handle = fopen("../light_switches/0.txt", "w");
if($status = 1){
$file_contents = "0";
fwrite($file_handle, $file_contents);
}
else if($status = 0){
$file_contents = "1";
fwrite($file_handle, $file_contents);
}
fclose($file_handle);
?>
readfile returns the number of bytes read from the file, but not the file content.
You could use file_get_contents to get the file content, and use file_put_contents to write:
$path = "../light_switches/0.txt";
file_put_contents($path, file_get_contents($path) === '0' ? '1' : '0');
Use file_put_contents(). It is the same as calling fopen(), fwrite(), and fclose(). Use the flag FILE_APPEND
file_put_contents($filepath, $content, FILE_APPEND);
PHP Documentation
EDIT
I have reworked this code. If all you are doing is trying to switch the 0 and 1 of the txt file, this code is working. Simply just replace the file path with yours.
<?php
$file_path = '0.txt';
if (!file_exists($file_path)) {
echo 'File does not exist';
exit;
}
$currentContent = file_get_contents($file_path);
echo $currentContent;
if ($currentContent == '0') {
$newContent = '1';
}
elseif ($currentContent == '1') {
$newContent = '0';
}
else {
//Start the file fresh if it found extra lines for example
$newContent = '0';
}
file_put_contents($file_path, $newContent);
?>
I am posting a string xml data to a php page hosted in IIS. All I want is to be able to read the string data that I receive in php page and write it to a file. But am not able to achieve the same. Here is my code:-
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
echo "Hello\n";
$somecontent = print_r($_POST, TRUE);
$my_file = "resp.txt";
$handle = fopen($my_file, "w") or die('Cannot open file: '.$my_file);
fwrite($handle, $somecontent);
}else {
echo "Error\n";
}
?>
But am not able to create the file or read the POST contents. I would be glad if someone would figure out how to solve this.
if you know the input field name then use this.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
echo "Hello\n";
$somecontent = array();
$somecontent[] = $_POST['firstname'];
$somecontent[] = $_POST['lastname'];
//etc...
$my_file = "resp.txt";
$handle = fopen($my_file, "wb") or die('Cannot open file: '.$my_file);
fwrite($handle, $somecontent);
fclose($handle);
}
else {
echo "Error\n";
}
?>
does this work?
if(!empty($_POST){
file_put_contents('resp.txt', serialize($_POST));
}