I use voice xml to select logo or movie. Put logo.svg or movie.php in the txt file. Then I use php to load the logo.svg or movie.php to the $theData.
<?php
ini_set("display_errors", true);
// define your file name as a variable
$myFile = "voice.txt";
// get a FILE HANDLE to the file: 'r' means set permission to read
$fh = fopen($myFile, 'r');
//read the entire file and store the contents in the variable $theData
$theData = fread($fh, filesize($myFile));
// close the file so it will be OK to use later
fclose($fh);
//echo whatever is in the file back to the browser
echo $theData;
// PART 2
// get a FILE HANDLE to the file: 'a' means set permission to read
$fh = fopen($myFile, 'w') or die("Error trying to open with a");
$noData = "no msgs";
//read the entire file and store the contents in the variable $theData
$theData = fwrite($fh, $noData);
// close the file so it will be OK to use later
fclose($fh)
?>
Now I want to show either of them on the webs.
I use:
<input class="logo" type="button" value="Call (832) 271-6319 " onclick="doAjaxCall(' ');"/>
How can I do it? Should I use javascript?
I'm not sure what you want to achieve.
The onclick allows you to add a javascript function like
<input onclick="myFunction()" />
You can make an ajax call from this function.
Or you can use jquery and attach a function to the input click event.
$(".logo").click(function(){
.....
});
Related
I need to be able to change text in a text file that is used to generate an HREF attribute of a link in my page through a form (i.e. the user puts the link into form and the link in HTML will change). So far I made that form and one PHP script. It is working, except that it won't just change that HREF attribute but it's making a whole new link (i.e. <img src="web/assets/images/youtube.png" alt="YT" width="42" height="42" border="0">) in the HTML.
Its working like that - when you put link into form, that script will write that link into the TXT file, then on that page where I need to have that HREF is another script that takes out link from a text file. Here is the PHP:
<?php
$subor = #file('youtube.txt');
$lines = sizeof($subor);
$count = $lines/1;
for ($i = 0; $i < $count; $i++)
{
?>
<a href="<?php echo ($subor[$i * 1 + 0]) ?>"><img
src="web/assets/images/youtube.png" alt="YT" width="42" height="42"
border="0"></a>
<?php
}
?>
And this is script that is writing the link from form into the text file:
<?php
$myFile = "youtube.txt";
if(isset($_POST['flag']) && !empty($_POST['flag'])) {
$fileWrite = $_POST['flag'] . "\n";
}
if($fileWrite) {
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
?>
<?php
header( 'Location: http://kraj.com/home.php' ) ;
?>
So how to edit that code so it won't create another but it will change the href inside that one ?
Mode parameter
So how to edit that code so it wont create another but it will change href inside that one ?
Change the second parameter (i.e. mode) passed to fopen() to 'w'. That way the pointer will be placed at the beginning of the file, instead of having the pointer at the end of the file (for appending).
$fh = fopen($myFile, "w") or die("can't open file"); //Make sure you have permission
See a demonstration of this in this playground example.
File_put_contents()
A simpler technique would be to use file_put_contents(). Then the following three lines
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
could be replaced with:
$numberOfBytesWritten = file_put_contents($myFile, $fileWrite);
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 have this issue, I would like to create a new file, with another name.
For now I have the file CLPRE.txt opened and saving the changes in the same file, I would like to create a new file from the original one like this
pseudo-code:
$unique= sha1( uniqid(phc) );
$newFile = $unique.CLPRE.txt
The actual code is resumed with this:
$myFile = $loja."/CL.xml";
$fh = fopen($myFile, 'w') or die("can't open");
$pre= file_get_contents('CLPRE.txt');
$writeThis = "some text";
fwrite($fh, $pre.$writeThis);
fclose($fh);
use file_put_contents
This function is identical to calling fopen(), fwrite() and fclose()
successively to write data to a file.
If filename does not exist, the file is created. Otherwise, the
existing file is overwritten, unless the FILE_APPEND flag is set.
Umm you're basically doing it already, you just need to do another call and create it via:
$new = fopen($unique.'CLPRE.txt', 'w') or die("can't open");
then add what you want into it, and close with:
fwrite($new , $pre.$writeThis);
fclose($new );
Reference to fopen() - http://www.decompile.com/cpp/faq/fopen_write_append.htm
I have this script on one free PHP-supporting server:
<html>
<body>
<?php
$file = fopen("lidn.txt","a");
fclose($file);
?>
</body>
</html>
It creates the file lidn.txt, but it's empty.
How can I create a file and write something into it,
for example the line "Cats chase mice"?
You can use a higher-level function like:
file_put_contents($filename, $content);
which is identical to calling fopen(), fwrite(), and fclose() successively to write data to a file.
Docs: file_put_contents
Consider fwrite():
<?php
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase');
fwrite($fp, 'mice');
fclose($fp);
http://php.net/manual/en/function.fwrite.php
$text = "Cats chase mice";
$filename = "somefile.txt";
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);
You use fwrite()
It is easy to write file :
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
Here are the steps:
Open the file
Write to the file
Close the file
$select = "data what we trying to store in a file";
$file = fopen("/var/www/htdocs/folder/test.txt", "w");
fwrite($file, $select->__toString());
fclose($file);
I use the following code to write files on my web directory.
write_file.html
<form action="file.php"method="post">
<textarea name="code">Code goes here</textarea>
<input type="submit"value="submit">
</form>
write_file.php
<?php
// strip slashes before putting the form data into target file
$cd = stripslashes($_POST['code']);
// Show the msg, if the code string is empty
if (empty($cd))
echo "Nothing to write";
// if the code string is not empty then open the target file and put form data in it
else
{
$file = fopen("demo.php", "w");
echo fwrite($file, $cd);
// show a success msg
echo "data successfully entered";
fclose($file);
}
?>
This is a working script. be sure to change the url in the form action and the target file in fopen() function if you want to use it on your site.
In order to write to a file in PHP you need to go through the following steps:
Open the file
Write to the file
Close the file
$select = "data what we trying to store in a file";
$file = fopen("/var/www/htdocs/folder/test.txt", "a");
fwrite($file , $select->__toString());
fclose($file );
fwrite() is a smidgen faster and file_put_contents() is just a wrapper around those three methods anyway, so you would lose the overhead.
Article
file_put_contents(file,data,mode,context):
The file_put_contents writes a string to a file.
This function follows these rules when accessing a file.If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename
Create the file if it does not exist then Open the file and Lock the file if LOCK_EX is set and If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
Write the data into the file and Close the file and release any locks.
This function returns the number of the character written into the file on success, or FALSE on failure.
fwrite(file,string,length):
The fwrite writes to an open file.The function will stop at the end of the file or when it reaches the specified length,
whichever comes first.This function returns the number of bytes written or FALSE on failure.
I have:
<?php
$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
?>
but it overwrites the beginning of the file. How do I make it insert?
I'm not entirely sure of your question - do you want to write data and not have it over-write the beginning of an existing file, or write new data to the start of an existing file, keeping the existing content after it?
To insert text without over-writing the beginning of the file, you'll have to open it for appending (a+ rather than r+)
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
If you're trying to write to the start of the file, you'll have to read in the file contents (see file_get_contents) first, then write your new string followed by file contents to the output file.
$old_content = file_get_contents($file);
fwrite($file, $new_content."\n".$old_content);
The above approach will work with small files, but you may run into memory limits trying to read a large file in using file_get_conents. In this case, consider using rewind($file), which sets the file position indicator for handle to the beginning of the file stream.
Note when using rewind(), not to open the file with the a (or a+) options, as:
If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.
A working example for inserting in the middle of a file stream without overwriting, and without having to load the whole thing into a variable/memory:
function finsert($handle, $string, $bufferSize = 16384) {
$insertionPoint = ftell($handle);
// Create a temp file to stream into
$tempPath = tempnam(sys_get_temp_dir(), "file-chainer");
$lastPartHandle = fopen($tempPath, "w+");
// Read in everything from the insertion point and forward
while (!feof($handle)) {
fwrite($lastPartHandle, fread($handle, $bufferSize), $bufferSize);
}
// Rewind to the insertion point
fseek($handle, $insertionPoint);
// Rewind the temporary stream
rewind($lastPartHandle);
// Write back everything starting with the string to insert
fwrite($handle, $string);
while (!feof($lastPartHandle)) {
fwrite($handle, fread($lastPartHandle, $bufferSize), $bufferSize);
}
// Close the last part handle and delete it
fclose($lastPartHandle);
unlink($tempPath);
// Re-set pointer
fseek($handle, $insertionPoint + strlen($string));
}
$handle = fopen("file.txt", "w+");
fwrite($handle, "foobar");
rewind($handle);
finsert($handle, "baz");
// File stream is now: bazfoobar
Composer lib for it can be found here
You get the same opening the file for appending
<?php
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
?>
If you want to put your text at the beginning of the file, you'd have to read the file contents first like:
<?php
$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
$existingText = file_get_contents($file);
fwrite($file, $existingText . $_POST["lastname"]."\n");
}
fclose($file);
?>