I'm using this script to see the content of a .text file -
<?php
$file = fopen("Gin_List_Website.txt","r");
if(!file)
{
echo("ERROR:cant open file .. :(");
}
else
{
echo("opened the file .. :)");
$buff = fread ($file,filesize("Gin_List_Website.txt"));
print $buff;
echo $buff;
}
?>
But all I see is this line
opened the file .. :)
What am I doing wrong?
First you need to change if(!file) with if(!$file).
Here is more simple code example:
<?php
$file = fopen("Gin_list_Website.txt", "r") or die("Unable to open file!");
echo fread($file,filesize("Gin_list_Website.txt"));
fclose($file);
?>
Related
I have a file manager and I want to add an option of editing files (html,php,css), but if I try with fgets() it displays the page and its graphic. How to get only lines from file and then send them as response to ajax request.
This is what I tried so far:
<?php
$handle = fopen('/location/', "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
} else {
// error opening the file.
}
?>
Use
show_source("/location/file.php"); to get the source code.
You can refer it from W3School - PHP show_source() Function
If the file is on the same server you can use
$content = #file_get_contents($filename);
if($content){
echo $content;
}else{
echo 'File:"'.$filename.'" couldn\'t be found.';
}
I am new to php so I was trying to make a simple multiple choice quiz app , one question at a time so when the user click the submit button it goes to the next question so the questions are in different php files , so I was trying to store the answers in a simple answers.txt file using the fopen , fwrite functions , but the problem is that when I tried to answer the quiz questions myself .. it doesnt create the answers.txt file , so I created it manually but it remains empty and here is the first questions' php code :
<?php
if (isset($_GET['q1']) && !empty($_GET['q1'])) {
$answer1 = $_GET['q1'];
$heranswers = fopen("Nanswers.txt ", "a+");
fwrite($heranswers, $answer1);
}
?>
so what's wrong with this ?
Add the following code in the if loop
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
die(error_get_last());
}
// Write $answer1 to our opened file.
if (fwrite($handle, $answer1) === FALSE) {
echo "Cannot write to file ($filename)";
die(error_get_last());
}
echo "Success, wrote ($answer1) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
Atleast you will find the error
make sure file permission .. it should be writable...
<?php
$answer1 = 10;
$filename = "Nanswers.txt";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a+')) {
echo "Cannot open file ($filename)";
die(error_get_last());
}
// Write $answer1 to our opened file.
if (fwrite($handle, $answer1) === FALSE) {
echo "Cannot write to file ($filename)";
die(error_get_last());
}
echo "Success, wrote ($answer1) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
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?
i have this php code:
<?php
echo ("Setting up data...");
$today = date("YmdHi");
$wtoday = $today
$im = $_GET["im"];
$fim = "tips/$today/im.txt";
$fwtoday = "tips/$today/today.txt";
?>
<?php
$fp = fopen ($fwtoday, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $wtoday);
fclose ($fp);
echo ("Today written");
}
else {
echo ("Today was not written");
}
?>
<?php
$fp = fopen ($fim, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $im);
fclose ($fp);
echo ("Im written");
}
else {
echo ("Im was not written");
}
?>
Finaly Today and Im was not written, where is my error ???
i dont think that have to do with file permissions.
i forgot to write about $fwtoday = "tips/$today/today.txt"; in the post, still not working.
Insert these lines to the front of your file, and share given errors:
error_reporting(E_ALL);
ini_set('display_errors','On');
$wtoday = $today
Missing semicolon, parse error.
That asice, you appear to be attempting to open a filename stored in the variable $fwtoday, which you don't seem to have defined anywhere.
I don't see the definition of $fwtoday. Also you don't need to do this:
?>
<?php
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));
}