How to insert some text in middle of file in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to insert some text middle of the file in php,
is that ay method available to insert text in middle of file content.
not exactly middle of the file.,
search a keyword, then we need to insert before or after the content,
if the search keyword found more than one time in file, then wat would be happen

Using fopen, fseek, ftell, fwrite, and fclose:
// Create the file handler
$file = fopen("filename", "r+");
// Seek to the end
fseek($file, SEEK_END, 0);
// Get and save that position
$filesize = ftell($file);
// Seek to half the length of the file
fseek($file, SEEK_SET, $filesize / 2);
// Write your data
fwrite($file, "Data");
// Close the file handler
fclose($file);

fopen()
fseek() <-- What you want I'd imagine
fwrite()
fclose()

open source file
open new destination file
copy "first part" of source file into destination file
add new content to destination file
copy "last part" of source file into destination file
close both files
delete original file
rename new file

fopen, then fseek, then fwrite

Related

How to search file with pattern [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a folder with many file , and i need to perform a deletion with certain file
and those file have a pattern like
messages.bm.inc.php
messages.cn.inc.php
messages.en.inc.php
Those file are dynamically created , but the pattern is there
Before this i normally delete my file with below code , and repeat it
$filename="messages.en.inc.php";
if (file_exists($filename)) {
unlink($filename);
}
Now i having a more dynamic situation , i need search through those file with the patern and delete it , please suggest a way to do , thanks
$files = glob("path_to_your_files/messages.*.inc.php ");
array_map('unlink', $files);
By glob you will get all your files from folder by specified pattern, array_map will implement unlink function for array of matched files.
foreach (glob("messages.*.inc.php") as $filename) {
unlink($filename);
}
Use the PHP glob() function to get the list of the files by pattern and delete using the loop.

New File in Php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do you create a new file in php. I am trying to use the fopen function. But as far as I can tell that only works if the file already exists. I am interesting in
Opening a file if it exists
If it does not exist create a new text file
Read and writing to the file.
fopen() will open a non existing file if you pass it a mode flag:
fopen("myfile.txt", "w"); //places the pointer at 0 and overwrites any existing data or creates new
fopen("myfile.txt", "w+"); //opens for writing and reading
the file_put_contents() function will dump data into a file:
$data = "my data block";
$myFile = "myFile.txt";
file_put_contents($myFile, $data);
From the docs: the possible arguments for file_put_contents() are:
filename Path to the file where to write the data.
data The data to write. Can be either a string, an array or a stream resource. If data
is a stream resource, the remaining buffer of that stream will be
copied to the specified file. This is similar with using
stream_copy_to_stream(). You can also specify the data parameter as a
single dimension array. This is equivalent to
file_put_contents($filename, implode('', $array)).
flags The value of
flags can be any combination of the following flags, joined with the
binary OR (|) operator.
To check if a file exists use the file_exists() function and fopen() or file_get_contents() if you want to suck the existing data into a variable:
if(file_exists($myFile))
{
fopen($myFile);
//do something
} else {
//use file_put_contents or fopen to dump file
}

how to open file and put string in the end of each line in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Please tell me how to open file and put string in the end of each line in php
Is this possible?
And who can make me understand this code?
f = open('./ampo.txt', 'r+')
with open('./ampo.txt') as infile:
for line in infile:
f.insert(0, 'EDF ')
f.close
In order to append to each line in the file you'll have to read the file in and then override it. I actually recommend writing to a different file and if you choose to do that just alter the file name in the fopen call.
$lines = file("./amp.txt");
$fp = fopen("./amp.txt", "w");
foreach($lines as $line) {
fwrite($fp, substr($line,0,-1) . "EDF" . substr($line,-1));
}
fclose($fp);
This is probably what you want
$myFile = "testFile.txt"; // The file
$fh = fopen($myFile, 'a') or die("can't open file"); // Open it
$stringData = "New Stuff 1\n"; // Your data
fwrite($fh, $stringData); // Write to file
$stringData = "New Stuff 2\n"; // Your data
fwrite($fh, $stringData); // Write again
fclose($fh); // Close it
the A (append) in fopen, will make sure things get appended to the file

PHP: Add line to text file from URL variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have never done any php, but I am trying to do something very simple.
So basically what I am trying to achieve is that add a line to already existing text file from url. To make it easier to understand, here's an example:
I have text file, what contains the following:
127.0.0.1-USA-Admin
127.0.0.1-SWE-Admin
127.0.0.1-CA-Admin
so after I go to link:
example.com/index.php?ip=127.0.0.1&Co=USA&Usr=Admin
The text file would be updated, and it would look like this:
127.0.0.1-USA-Admin
127.0.0.1-SWE-Admin
127.0.0.1-CA-Admin
127.0.0.1-USA-Admin
How to achieve this?
I am sorry if it's a beginner question, but I've never done anything in PHP.
I assume it has something to do with $_GET -thing.
Please help :)
Here it is,
$ip = $_GET["ip"];
$co = $_GET["co"];
$usr = $_GET["usr"];
$file = "yourfile.txt";
file_put_contents($file, "$ip-$co-$usr", FILE_APPEND | LOCK_EX);
For more information, please check
http://php.net/manual/en/function.file-put-contents.php
Lets say that the text file name is textfile.txt. Bellow is the php code:
<?php
$fh = fopen('textfile.txt', 'a');
fwrite($fh, $_GET['ip'].'-'.$_GET['Co'].'-'.$_GET['Usr']."\n\r");
fclose($fh);
?>
You've not given any code to correct/fix. And I'm not going to write it for you.
Here are the functions you'll most likely need:
You'll need a couple of functions.
file_put_contents http://php.net/manual/en/function.file-put-contents.php
To append to the file.
parse_url http://php.net/manual/en/function.parse-url.php
If you get stuck, paste your code into your question.
You can use file_put_contents:
file_put_contents(
'your-file.ext',
sprintf( "%s-%s-%s", $_GET['ip'], $_GET['Co'], $_GET['Usr'] ),
FILE_APPEND
)
The code should be
$contents = file("myfile.txt");
$contents[] = $_GET["ip"]."-".$_GET["Co"]."-".$_GET["Usr"];
file_put_contents('myfile.txt', $contents);

Adding to .txt file in PHP instead of overwriting it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this line of PHP code here
<?php
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
?>
Whenever
phpfile.php?blablabla
is queried it writes a query.txt with the parameter in it, in this case, blablabla.
BUT, when I do this, it deletes the past query.txt file and writes a totally new one.
I want the queries to be ADDED into the .txt file. So there can be as many queries and possible and every single value entered will be lined up in the .txt file..
For example
I want it so phpfile.php?test is visited, query.txt looks like this
test
after that, phpfile.php?test2 is visited
now query.txt looks like this
test
test2
and this goes on forever.
How do I do this?
p.s.: sorry, I'm totally a Java type. I'm an absolute beginner to PHP.
You wish to append to the file, so you should use something along the lines of
$handle = fopen("myfile.txt", "a");
fwrite ($handle, parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
fclose($handle);
You can find all the other read/write file open flags at the docs.
http://php.net/manual/en/function.fopen.php
It appears you can also just pass a flag with your existing code, as that will abstract the file handle open/close from you;
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), FILE_APPEND);
Add FILE_APPEND as 3rd argument to file_put_contents(). Example (.PHP_EOL to add new line):
$data = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY).PHP_EOL;
file_put_contents('query.txt', $data, FILE_APPEND);

Categories