i have inserted following link to pipe the emails.
link: public_html/emailer.php
i have script file in public_html folder, the following script i am using:
it should create a file in the directory, but its not working.
#!/public_html/ -q
<?php
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("pipemail.txt", "w");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
?>
this is how i am setting pipe to program path
This will work.
#!/usr/local/bin/php -q
<?php
$fd = fopen("php://stdin","r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd,1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("pipemail.txt", "w");
fwrite($fdw, $email);
fclose($fdw);
?>
Make sure it has unix line [\n] ending while you put the code in your server.
Also make sure the script has permission to execute!
Related
I am debugging a php script that will be called by a javascript function and that have POST arguments. I would like to test it in command line mode. I know it is possible doing it with the php -a option. But once in the interactive mode how to I set up the arguments? And how do I call my php? I can't neither remenber nor find how to do it.
My php script is:
<?php
$data = $_POST['string'];
$fname = $_POST['file'];
$dir=$_POST['dir'];
mkdir($dir);
$file = fopen($fname, 'w');
fwrite($file, $dir."\n");
fwrite($file, $data."\n");
fwrite($file, "/var/www/html/ChemAlive_app/SOFTWARE/utilities/"."\n");
fclose($file);
$saved = getenv("LD_LIBRARY_PATH"); // save old value
$newld = "/usr/local/NWChem/lib/"; // extra paths to add
if ($saved) { $newld .= ":$saved"; } // append old paths if any
putenv("LD_LIBRARY_PATH=$newld");
$saved = getenv("PATH"); // save old value
$newld = "/usr/local/NWChem/bin/"; // extra paths to add
if ($saved) { $newld .= ":$saved"; } // append old paths if any
putenv("PATH=$newld");
exec("cd $dir ; /var/www/html/ChemAlive_app/SOFTWARE/ChemAliveExec/ReactionThermo ".$fname);
?>
Thanks for your help.
you can use the following:
<?php
// check to see if php called from cmd line
if (php_sapi_name() == 'cli') {
//take vars from cmd line args argv[0] is script name
$data = $argv[1];
$fname = $argv[2];
$dir = $argv[3];
} else {
$data = $_POST['string'];
$fname = $_POST['file'];
$dir=$_POST['dir'];
}
then call the script on the command line with
/path/to/php /path/to/script.php 'some data' 'filename' 'dirname'
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
}
?>
I am using this piece of code to pipe emails:
#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
}
fclose($fd);
In emails I need 4 lines starting with
---BEGIN---
data here
more data
ending with
---END---
I tried with explode and strstr but I am getting everything after the --BEGIN---
It doesn't stop at ---END--- somehow.
How I can "say" to it that I only need what is between ---BEGIN--- and ---END--- ?
preg_replace('/^.*---BEGIN---(.+?)---END---.*$/s', '$1', $email);
I have emails piped to a PHP script, which looks like this:
#!/usr/local/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
$email .= fread($fd, 1024);
}
fclose($fd);
mail('test#test.com','From TEST','"' . $email . '"');
?>
This essentially sends me the entirety of the email.
I can't seem to find anything that doesn't rely on existing frameworks to read attachments.
Can anyone point me in the right direction for getting just the attachment data? The attachment will always be a CSV file.
I have a form that write its values to a text file. My problem is that when a user submits a form it over writes the value from the last users submission. What changes would i need to make in order to have it record each users submission and not over write it each time.
// This just names the file
$target_filename = "usersubmit_f456sd4f56sd4f.txt";
// Create an empty buffer
$message = "";
// This gets all the form keys (names) and values
foreach ($_POST as $key => $value)
$message .= "$key: $value\n";
// Put the date in
$message .= date("F j, Y, g:i a");
// Open the file and write it out
$fp = #fopen($target_filename,"wt");
if ($fp != NULL)
{
fputs($fp,$message);
fclose($fp);
}
Change
$fp = #fopen($target_filename,"wt");
TO
$fp = fopen($target_filename, 'a');
You can find references HERE... When using a it means to append... When file doesn't exists this function will force creation...
Hope it helps!
You can use this code:
<?php
$target_filename = "testFile.txt";
$fh = fopen($target_filename, 'r');
$theData = fread($fh, filesize($target_filename));
$fh = fopen($target_filename, 'w') or die("can't open file");
$message = $theData."\n";
foreach ($_POST as $key => $value)
$message .= "$key: $value\n";
//Put the date in
$message .= date("F j, Y, g:i a");
fwrite($fh, $message);
fclose($fh);
?>
Hope it works.
Thanks
Open the file in append mode:
$fp = fopen($target_filename, 'a');
You could also simply use file_put_contents():
file_put_contents($target_filename, $message, FILE_APPEND);
take this fopen() function or You can Go with php manual ......use this
// This just names the file
$target_filename = "usersubmit_f456sd4f56sd4f.txt";
// Create an empty buffer
$message = "";
// This gets all the form keys (names) and values
foreach ($_POST as $key => $value)
$message .= "$key: $value\n";
// Put the date in
$message .= date("F j, Y, g:i a");
// Open the file and write it out
$fp = fopen($target_filename, 'a');
if ($fp != NULL)
{
fputs($fp,$message);
fclose($fp);
}
a: Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already
try instead
<?php
$file = 'usersubmit_f456sd4f56sd4f.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
file_put_contents
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file
// Open the file and write it out
$fp = #fopen($target_filename,"wt");
if ($fp != NULL)
{
fseek($fp,0,SEEK_END);
fputs($fp,$message);
fclose($fp);
}
http://php.net/fseek