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"]
Related
When I request this URL:
http://example.com/csv_build.php?filename=test.txt&time=1485902100000&data=25
I expect the PHP code to create a file named : datafile.txt that will contain the following data:
test.txt,1485902100000,25<CR><LF>
For some reason, datafile.txt is not created.
Did I made a mistake ?
<?
# GRAB THE VARIABLES FROM THE URL
$File = 'datafile.txt';
$FileName = $_GET['filename'];
$HeureTronconMesure = $_GET['time'];
$DonneeCapteur = $_GET['data'];
# --------------------------------
$FICHIER = fopen($File, "a");
#ftruncate($FICHIER,0);
fputs($FICHIER, $FileName);
fputs($FICHIER , ",");
fputs($FICHIER, $HeureTronconMesure);
fputs($FICHIER , ",");
fputs($FICHIER, $DonneeCapteur);
fputs ($FICHIER , "\r\n");
fclose($FICHIER);
?>
You're not doing any error checking. Why do you assume everything will work okay? If you spot some errors, first thing I would check is file permissions.
<?php
# GRAB THE VARIABLES FROM THE URL
$File = str_replace("/", "_", $_GET["filename"]);
$FileName = $_GET['filename'];
$HeureTronconMesure = $_GET['time'];
$DonneeCapteur = $_GET['data'];
# --------------------------------
if (!$FICHIER = fopen($File, "a")) {
//there was an error opening the file, do something here
}
fputs($FICHIER, $FileName);
fputs($FICHIER , ",");
fputs($FICHIER, $HeureTronconMesure);
fputs($FICHIER , ",");
fputs($FICHIER, $DonneeCapteur);
fputs ($FICHIER , "\r\n");
fclose($FICHIER);
?>
Though really, I'd simplify this code immensely by using some shortcuts and concatenating strings.
<?php
// replace any slashes in the filename with underscores
$file = str_replace(["/", "\\"], "_", $_GET["filename"]);
// build a string out of your data
$data = "$_GET[filename],$_GET[time],$_GET[data]\r\n";
// write the data to the file, checking if it returns false
if (!file_put_contents($file, $data)) {
//there was an error writing the file, do something here
}
Note, never open your code with <?, it's been deprecated a very long time now. Use <?php.
So.. i have problems with this code :
$filename = "info.txt";
$tex = $_POST['Name'];
$text = $tex . $_POST['Surname'];
$fp = fopen ($filename, "w");
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
It saves ONLY 1 name & surname. after another person submits his info, previous information is lost.
Is it possible to save all information?
You have to change the mode for fopen :
change 'w' to 'a' (a for append)
You'll find more information about each mode right here:
http://us2.php.net/manual/en/function.fopen.php#refsect1-function.fopen-parameters
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 have an issue with writing and reading to text file.
I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:
Write to text file:
$fp = #fopen ("text1.txt", "r");
$fh = #fopen("text2.txt", 'a+');
if ($fp) {
//for each line in file
while(!feof($fp)) {
//push lines into array
$thisline = fgets($fp);
$thisline1 = trim($thisline);
$stringData = $thisline1. "\r\n";
fwrite($fh, $stringData);
fwrite($fh, "test");
}
}
fclose($fp);
fclose($fh);
Read from the written textfile
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
echo rtrim($kw[$i]);
}
But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.
How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.
I'm not sure whether you have issues with the even line numbers or with reading the file back in.
Here is the solution for the even line numbers.
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$myValue = rtrim($kw[$i]);
if(i % 2 == 0)
{
echo $myValue;
}
}
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