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";
}
?>
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'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);
?>
I have the following code to write data to a text file.
$somecontent = "data|data1|data2|data3";
$filename = 'test.txt';
// Let's make sure the file exists and is writable first.
IF (IS_WRITABLE($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
IF (!$handle = FOPEN($filename, 'a')) {
PRINT "Cannot open file ($filename)";
EXIT;
}
// Write $somecontent to our opened file.
IF (!FWRITE($handle, $somecontent)) {
PRINT "Cannot write to file ($filename)";
EXIT;
}
PRINT "Success, wrote ($somecontent) to file ($filename)";
FCLOSE($handle);
} ELSE {
PRINT "The file $filename is not writable";
}
Now I want this text file to only every have 10 lines of data and when a new line of data is added which is unique to the other lines then the last line of data is deleted and a new line of data is added.
From research I have found the following code however total no idea how to implement it on the above code.
check for duplicate value in text file/array with php
and also what is the easiest way to implement the following code?
<?
$inp = file('yourfile.name');
$out = fopen('yourfile.name','w');
for ($I=0;$i<count($inp)-1);$i++)
fwrite($out,$inp[$I]);
fclose($out)l
?>
Thanks for any help from a PHP newbie.
$file = fopen($filename, "r");
$names = array();
// Put the name part of each line in an array
while (!feof($file)) {
$line_data = explode("|", $fgets($file));
$names[] = $line_data[0]
}
$data_to_add = "name|image|price|link"
$data_name = "name" // I'm assuming you have this in a variable somewhere
// If the new data does not exist in the array
if(!in_array($data_name, $names)) {
unset($lines[9]); // delete the 10th line
array_unshift($lines, $data_to_add); // Put new data at the front of the array
// Write the new array to the file
file_put_contents($filename, implode("\n", $lines));
}
I have a basic php script that will write/log to a text document
<?php
$filename = 'php_log.log';
$somecontent = 'writing with fwrite!'.PHP_EOL;
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
error_log('logging with error_log!'.PHP_EOL,3,'php_log.log');
?>
From CLI, if i do php test.php and check php_log.log, I see both writes.
But when I try php test.php &, there is nothing written to php_log.log
Is there some php setting I am unaware of?
Thanks!
Possibly the absence of locking.
Why are writing to the file manually and then with error_log?
Consider file_put_contents() with FILE_APPEND|FILE_LOCK instead. That appends just to the end of the file and takes care of locking.
Enable error_reporting and redirect stderr to a separate file to find out more.
php test.php 2> php_errors.txt &
I am working with a client on getting a gzip from their webservice. I am able to get a response with my following call:
$response = $client->call('branchzipdata', $param);
$filename = "test.gzip";
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $response) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
Now when I attempt to write that a file, such as 'test.gzip', I am unable to open it afterwards... most likely because I am doing something horrible wrong. Any insight would be appreciated.
EDIT:
For some reason I was saving the file as '.gzip' instead of '.gz'... So in order to have it work I now have:
$response = $client->call('call', $param);
$content = base64_decode($response);
$filename = "output_zip.gz";
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
echo system("gzip -d $filename");
(Edited based on the comments)
If the return value is base64-encoded, you need to base64-decode it before you write it to the file. Alternatively you could write it out to a file which you then base64-decode to another file before trying to open it, but that seems a bit pointless compared with just decoding it when you first get it.