Changing text in file that generates HREF attribute in HTML with PHP - php

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

Related

How to submit a POST using <a href=""> tag?

I realize we need forms to submit a POST or perhaps with the help of a button. However, how do we do it using tags? Example code:
<?php
$myfile = fopen("pictures.txt", "r") or die("Unable to open file!");
while(!feof($myfile)){
$pic_name= fgets($myfile);
echo '<div class="grid-item">';
echo '<img src="' . $pic_name . '.jpg">';
echo '</div>';
}
?>
I'm trying use PHP to read a text file for a number of variables (names of each picture that I've saved). From the text file, this PHP will then convert the variables into multiple images (for each variables). Upon clicking the image, this should send the name of the picture to the next PHP site so the picture is displayed in full screen. Assume this is some kind of photo library thing that I'm trying to do. I've tried searching elsewhere but couldn't get an answer for this.
you can use get method instead for sending data for eg.
"form.php?name1=value1&name2=value2"
<?php
$myfile = fopen("pictures.txt", "r") or die("Unable to open file!");
while(!feof($myfile)){
$pic_name= fgets($myfile);
echo '<div class="grid-item">';
echo '';
echo '</div>';
}
?>

Bug fread in a txt file, read only one time

I'm creating a code to display the name of a server with enterprise rules, So for don't use Mysql i try a new things (for me) use php to read and rewrite files, that work perfectly for one part of my code and work perfectly but for the second he only read one time, and when i do a f5 the code don't increment.
He rewrite correctly because my file was at 000 and become 001
I try to use file() but he is disable since 7.0, try to use SplFileObject but it don't want to display anything and i don't like it because i understand nothing when i use it so i come back to fopen(),fread() and fwrite() and that don't work. I'm inPHP 7.3.1
The code that works :
<?php
if ( isset($_POST) AND !empty($_POST) ) {
$nom = "./config.txt";
$filez = fopen($nom, "r") or die("Unable to open file!");
$i = fread($filez,filesize($nom));
$year = getdate();
$idy = substr($year[year], 2);
$fichier = fopen("./resultsrv.txt", "w") or die("Unable to write file!");
for ($z; $z<$_POST['nbr']+1 ; $z++) {
$id = sprintf("%04d", $i+$z);
$nome = $_POST['type'].$_POST['OS'].$idy.$id."<br>" ;
echo $nome;
$nomewout = str_replace("<br>", ";", $nome);
fwrite($fichier,$nomewout);
}
$handle = fopen("./config.txt", "w") or die("Unable to write file!");
fwrite($handle,$id);
fclose($fichier);
fclose($handle);
}
?>
and the one that doesn't work because he doesn't increment :
<?php
if ( isset($_POST) AND !empty($_POST) ) {
$fileName = 'confchass.txt';
$read = fopen($fileName,"r");
$fn = fopen($fileName,"w+");
$i = fread($read,filesize($fileName));
$id = sprintf("%03d", $i+1);
echo "<div align='center'><h1>Le Chassis</h1>";
echo $_POST['Marque'].$_POST['DC'].$id;
echo "</div>";
fwrite($fn,$id);
fclose($read);
fclose($fn);
}
?>
I want he output a thing like XXXXXX001 and when i refresh or do a new POST from my forms he output XXXXXX002 and XXXXXX003 .... But he actualy output only XXXXXX001
The problem is that you open the file for reading and then for writing. But from the manual...
'w+' Open for reading and writing; place the file pointer at the
beginning of the file and truncate the file to zero length. If the
file does not exist, attempt to create it.
So this will blank out the file before you read the value from it.
To fix this (using your current method, you should read the value, then open it for writing and write the new value...
$read = fopen($fileName,"r");
$i = fread($read,filesize($fileName));
fclose($read);
$id = sprintf("%03d", $i+1);
echo "<div align='center'><h1>Le Chassis</h1>";
echo $id;
echo "</div>";
$fn = fopen($fileName,"w+");
fwrite($fn,$id);
fclose($fn);
You could shorten this by using file_get_contents() and file_put_contents().

How to use txt content to onclick="doAjaxCall(' ')?

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(){
.....
});

How to get text from txt file

I'm creating a html template with notification under nav bar , and admin can change that notification from the system the text of notification bar will be from notetxt file from the same location path where index.html is located i ave tried
<?php
foreach (glob("note.txt") as $filename) {
readfile($filename);
}
?>
and many other way but nothing happens it still stay blank
You are not echoing out the content of the textfile.
do it like this:
$myFile = "note.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
This will output your content of the file.
i'm using this code in pure html file
You can't use PHP functions in plain HTML file. MUST be written in a PHP file.
You have now in your code:
<span>
<!--?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?-->
</span>
Try with the examples above in a proper PHP file... then must work.
you can use file_get_contents function,
try something like this :
<?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?>
It's very simple use file_get_contents();
<?= file_exists('note.txt') ? file_get_contents("note.txt") : "file doesn't exists"; ?>
That is all what you need. file_get_contents() get the content of file and returns it. I've also checked if file exists because it may be your problem. Also make sure you have proper rights to read the file(CHMOD) and file is not empty.

PHP unlink OR rewrite own/current file by itself

Task: Cut or erase a file after first walk-through.
i have an install file called "index.php" which creates another php file.
<?
/* here some code*/
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?php \n
echo 'hallo, *very very long text*'; \n
?>";
fwrite($fh, $stringData);
/*herecut"/
/*here some code */
after the creation of the new file this file is called and i intent to erase the
filecreation call since it is very long and only needed on first install.
i therefor add to the above code
echo 'hallo, *very very long text*'; \n
***$new= file_get_contents('index.php'); \n
$findme = 'habanot';
$pos = strpos($new, $findme);
if ($pos === false) {
$marker='herecut';\n
$new=strstr($new,$marker);\n
$new='<?php \n /*habanot*/\n'.$new;\n
$fh = fopen('index.php', 'w') or die 'cant open file');
$stringData = $new;
fwrite($fh, $stringData);
fclose($fh);***
?>";
fwrite($fh, $stringData);]}
Isnt there an easier way or a function to modify the current file or even "self destroy" a file after first call?
Regards
EDIT: found the way to edit, sorry to zaf
unlink(__FILE__);
can be used to delete the "helper file" after execution.
unlink(__FILE__);
for the "helper" file seems necessary since i cant find a way to modify the php-file inuse/process.
Most self-installing PHP sites use an install.php to perform the initial set-up. When the install is verified, you would redirect to removeinstall.php which would call unlink() on each installation file to clear them all out.
This does leave behind the removeinstall.php, but has the benefit of not polluting any of the "live code" with installation removal code.
removeinstall.php would simply contain the unlink statements...
if (file_exists('install.php')) {
unlink('install.php');
}
If you don't want to leave behind the removeinstall.php, you could have a conditional call in a different file... for example index.php?removeinstallation=1 or similar.

Categories