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);
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
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
}
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);
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
This one may be a bit of a doozy.
I have 2 sets of files in separate folders:
SET 1:
celeb1.png
celeb2.png
celeb3.png
celeb4.png
celeb5.png
etc
The user selects an image (radio) and I then set this as a get variable on the url:
Now, I want to match another file.
In another folder, I have a set like this:
SET 2:
celeb1-24_59_250_300.png
celeb2-35_67_200_250.png
celeb3-54_87_300_400.png
celeb4-88_98_250_350.png
celeb5-87_43_300_400.png
etc
I want to use the GET variable (i.e.) celeb1.png, and then select the full filename of the corresponding file, based on the first 5 characters.
In other words if $_GET['celebimg'] is 'celeb1.png', I want to set a second variable (let's say $overlay) to string 'celeb1-24_59_250_300.png'
Any idea on how to achieve that?
I could obviously do a switch, but that means I would have to update the code every time a new celeb image is uploaded.
Is there any dynamic way to achieve this?
Thanks
JG
You can try something like this. Since we haven't seen what you have tried so far. This is just an illustration. However the code has been tested.
<?php
$file='celeb3.png'; // Here is where you will get the param $_GET['set1']
$set2=array(
'celeb1-24_59_250_300.png',
'celeb2-35_67_200_250.png',
'celeb3-54_87_300_400.png',
'celeb4-88_98_250_350.png',
'celeb5-87_43_300_400.png'
);
$set1 = explode('.',$file);
//echo $set1[0];//celeb3
foreach($set2 as $val)
{
if($set1[0]==substr($val,0,strlen($set1[0])))
{
echo $val;//celeb3-54_87_300_400.png
break;
}
}
OUTPUT : celeb3-54_87_300_400.png
Use glob to search a folder
//Get the GET variable
$cimg = $_GET['celebimg'];
//split it so we can just get the name without extention
$nameparts = explode(".",$cimg);
//glob will try to find matches to the string passed
$matches = glob("/dir/path/{$nameparts[0]}-*.png");
$matches will be an array of matched files
you could use other file functions like scandir or readdir etc but those do not filter out the files so you would have to do it.
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