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);
Related
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.
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'm new in File handling. So I was wondering how would I go about to write a php file with php?
Let's say I want to make a php script which creates a file which contains the following code:
PHP File TO be Created
<?php
$item = "takes a variable from current file and put it here";
?>
Let's say I have a php which took the following from my form:
Code which takes a variable and writes it to another php file, exactly as it is
<?php
$item = $_GET['shirts'];
//Code which writes to Php File above
?>
I have no idea how to do this. Please be very explicit when you explain.
Thanks :)
Like this:
$var='that';
file_put_contents('newfile.php','<?php echo "thisthat' . $var . '"; ?>');
Note that if $var is coming from user input in any way you will need to be extremely careful to sanitize it so a hacker can't just do anything he wants on your server.
Documentation for file_put_contents here: php.net/file_put_contents
file_put_contents('fileToCreate.php', '<?php' . "\n" . '$item = "' . $item . '";' . "\n" . '?>')
viz here: php.net/file_put_contents
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
if i output my URI i also get the folder in which it is located at.
echo $_SERVER['REQUEST_URI']; //outputs /abcgetoutofjail/admin.php?make_account
im on localhost right now, and the website is under folder in htdocs abcgetoutofjail
i need only admin.php?make_account
i need the last part of the url in any way
how can i achieve that with either another way of getting uri or using a string function to cut SERVER URI
function DescriptionAndTitle($uri)
{
echo $uri;
}
In Php, you can achieve this using explode & count function.
$uri = '/some-dir/yourpage.php?q=bogus&n=10';
$uri = explode('/', $uri);
$len = count($uri);
print $uri[$len-1];
To get the last part of a URL I usually do something like the following:
$url = parse_url($_SERVER['REQUEST_URI']);
$partsofurl = explode("/", $url['path']);
$partyouwant = end($partsofurl);
$needed_part = end(explode('/', $_SERVER['REQUEST_URI']));
I believe this is what you're looking for:
$dir = explode('/', dirname($_SERVER['PHP_SELF']));
$dir = '/'.end($dir);
echo $dir;
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);
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 text file on my local server. I want to read content data of this text file and write it into textbox in a html .
Could you please help me?
$data = file_get_contents('yourfile.txt');
echo '<textarea>', htmlspecialchars($data), '</textarea>';
The file_get_contents() loads the data, and then you just need to echo it. Using htmlspecialchars() takes care of escaping any of the data, as needed in the context of HTML.
$textAreaContent = "";
$fp = fopen("read_file.txt", "r");
// If file exist and opened
if($fp)
{
while (($buffer = fgets($fp, 1024)) !== false)
$testAreaContent .= $buffer;
}