Error on writing files in php - php

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

Related

Why can't I see my .txt file from this php script

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);
?>

Need help - php save .txt

I need your help.
I need to every time the code stores the information in txt file, then each new record to the new line and what should be done to all be numbered?
<?php
$txt = "data.txt";
if (isset($_POST['Password'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['Password'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
Added some comments to explain the changes.
<?php
$file = "data.txt"; // check if both fields are set
$fh = fopen($file, 'a+'); //open the file for reading, writing and put the pointer at the end of file.
$word=md5(rand(1,10)); //random word generator for testing
fwrite($fh,$word."\n"); // Write information to the file add a new line to the end of the word.
rewind($fh); //return the pointer to the start of the text file.
$lines = explode("\n",trim(fread($fh, filesize($file)))); // create an array of lines.
foreach($lines as $key=>$line){ // iterate over each line.
echo $key." : ".$line."<br>";
}
fclose($fh); // Close the file
?>
PHP
fopen
fread
explode
You can do like this in a more simpler way..
<?php
$txt = "data.txt";
if (isset($_POST['Password']) && file_exists($txt))
{
file_put_contents($txt,$_POST['Password'],FILE_APPEND);
}
?>
we open file to write into it ,you must make handle to a+ like php doc
So your code will be :
<?php
$fileName = "data.txt"; // change variable name to file name
if (isset($_POST['Password'])) { // check if both fields are set
$file = fopen($fileName, 'a+'); // set handler to a+
$txt=$_POST['Password'];
fwrite($file,$txt); // Write information to the file
fclose($file); // Close the file
}
?>

PHP create a text file - difficulty in creating a text file

busy studying php with a book called "php and mysql web development 4th edition". i have trouble with the following code. i am trying to create a text file. i'm testing all code on a live server
i get the following errors:
Warning: fopen(/home/truevvky/public_html/../orders/orders.txt): failed to open stream: No such file or directory in /home/truevvky/public_html/test/processorder.php on line 60
Warning: flock() expects parameter 1 to be resource, boolean given in /home/truevvky/public_html/test/processorder.php on line 62.
the idea is to create a new text file
<?php
//create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F Y');
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";
echo "<p>Your order is as follows: </p>";
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "Items ordered: ".$totalqty."<br />";
if ($totalqty == 0) {
echo "You did not order anything on the previous page!<br />";
} else{
if ($tireqty > 0) {
echo $tireqty." tires<br />";
}
if ($oilqty > 0) {
echo $oilqty." bottles of oil<br />";
}
if ($sparkqty > 0) {
echo $sparkqty." spark plugs<br />";
}
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2, '.',' ');
echo "<p>Total of order is $".$totalamount."</p>";
echo "<p>Address to ship to is ".$address."</p>";
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
.$sparkqty." spark plugs\t\$".$totalamount."\t".$address."\n";
// open file for appending
# $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
flock($fp, LOCK_EX);
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
?>
</body>
</html>
I'm working on the same book and found the solution to his problem. I know this is old and maybe he figured this out, but wanted to put my answer for anyone else who encounters this.
for some reason in the book he puts flock before we even know if the file exist that's what is causing the error. flock() just puts a lock so that we can write LOCK_EX and fwrite() then writes whatever we output and LOCK_UN releases the lock.
for $DOCUMENT_ROOT mine with WAMP looked like this "C:/wamp/book/orders/orders.txt" so in the code remove the "..".
// open file for appending
# $fp = fopen("$DOCUMENT_ROOT/orders/orders.txt", 'ab');
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
//you move flock down here
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
Opening file for appending works with file that exists. I would personally do something like
$path = "$DOCUMENT_ROOT/../orders/orders.txt";
$content = "Okay here are my contents";
$fp = null;
if(file_exists($path))
{
$fp = fopen($path, 'ab');
}
else
{
$fp = fopen("myText.txt","wb");
}
fwrite($fp,$content);
fclose($fp);
Maybe you did not create "orders" folder in "$DOCUMENT_ROOT/../" path. You should understand fopen('filepath', 'ab') just can create the specific file. If the path was not complete (can't find the "orders" folder), it won't work. So you can make the folder manually first, then test the .php
echo $DOCUMENT_ROOT
you could see your server's root address, mine is D:/AppServ/www.
If you use "$DOCUMENT_ROOT/../orders/orders.txt" you will get address: D:/AppServ/orders/orders.txt. Notice that you should be sure that you have a file folder named orders. So we can see that .. is to mean the parent directory of the document root directory, the parent directory is D:/AppServ.
Please make sure that the web server user can write a file on '/home/truevvky/public_html/../orders/' directory.
The fopen statement should be:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'w');
Your way assumes the file exists.
By the way the 'b' you included in the second argument means you want to write the data in binary. If that's the case then fopen should be:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'wb');

Creating and writing a file in WordPress using PHP

I would like to create a file to cache my lookups (coordinates and so on). I don't know why but I cannot create and write to it within WordPress. I am using this code for a try:
<?php
$filename = 'sitevisitors.txt';
if (file_exists($filename))
{
$count = file(TEMPLATEPATH . 'sitevisitors.txt');
$count[0] ++;
$fp = fopen(TEMPLATEPATH . "sitevisitors.txt", "w");
fputs ($fp, "$count[0]");
fclose ($fp);
echo $count[0];
}
else
{
$fh = fopen(TEMPLATEPATH . "sitevisitors.txt", "w");
if($fh==false)
die("unable to create file");
fputs ($fh, 1);
fclose ($fh);
$count = file(TEMPLATEPATH . 'sitevisitors.txt');
echo $count[0];
}
?>
I do not get any error message, but the file "sitevisitors.txt" is not created and update and does not appear on my server.
What am I doing wrong? The path should be ok.
My server host confirms that I have full privileges.
This code works beautifully outside WordPress...
Any suggestion is welcome!
Cheers, Marina
The TEMPLATEPATH constant doesn't have a slash at the end, you should use it like:
$fh = fopen(TEMPLATEPATH . "/sitevisitors.txt", "w");
notice the slash just before the filename

Php upload form userinput = filename

Ok building a php webform need one of the entries to be the filename written.
How can I achieve this?
Here's my php code...
$filename = "output.txt"; #Must CHMOD to 666
$text = $_POST['bin'];
$text2 = $_POST['pcn'];
$text3 = $_POST['gnum'];
$text4 = $_POST['memid'];
$text5 = $_POST['urlen'];
$text6 = $_POST['urles'];
$text7 = $_POST['tlogo'];
# Form must use POST. if it uses GET, use the line below:
#$text = $_GET['theformfieldname']; #POST is the preferred method
$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, "$text\r\n");
fwrite ($fp, "$text2\r\n");
fwrite ($fp, "$text3\r\n");
fwrite ($fp, "$text4\r\n");
fwrite ($fp, "$text5\r\n");
fwrite ($fp, "$text6\r\n");
fwrite ($fp, "$text7\r\n");
fclose ($fp);
header("Location: logo.html");
}
else {
echo ("There was an error please submit your request again");
}
?>
ok need $filename = "output.txt";
to be from the input $text3 = $_POST['gnum'];
something along the lines of:
$filename = $_POST['gnum'].txt;
but this wont work..
Thanks in advance,
Joe
instead of
$filename = $_POST['gnum'].txt;
you should use
$filename = $_POST['gnum'].".txt";
I'm not super clear on what you're asking, but I think you want to know how to appent '.txt' to the end of the $_GET variable?
If that's the case, you just need to do this:
$text3 = $_POST['gnum'] . '.txt';
Use this for getting the file name.
$_FILES["file"]["name"]

Categories