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
Related
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
I have a textfile which contain words like that:
Yigit Ozkavci 19
Efe Ozkavci 18
Yeni Bar 35
.
.
.
They are exist in database and also in textfile, what I want to do is to read those datas and get them in seperate variables, I know how to read a file line by line, but with a foreach loop can I echo them seperately and send data to another php file ?
Why not just make use of file() ? It reads entire content inside an array.
<?php
$arr = file('yourfile.txt');
You can access Yigit Ozkavci 19 by echo $arr[0]; and others so on...
If you want to process it... you can just run it on foreach like this
foreach($arr as $val)
{
echo strtoupper($val); // Just a demonstration ..
}
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";
}
?>
Hope This Helps :)
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 I make the following url to be read:
localhost/index.php?errormsg=wrongpassword
so I want it to set the variable $errormsg to 'wrongpassword' how do I do that? I am pretty sure there is a way using $_GET or $_POST?
You want to use $_GET['errormsg'].
Something like this should do:
if isset($_GET['errormsg'])
{
echo $_GET['errormsg'];
}
$errormsg = $_GET['errormsg']
This should do the trick for you
$_GET['errormsg'] is the answer of your question.
print_r($_GET)
to see all variables in the URL. in this case you want
$_GET['errormsg']
You were as close as ever. A single google would've yielded the answer to you :)
http://php.net/manual/en/reserved.variables.get.php
<?php
// use htmlspecialchars() to clean up url-encoded characters
$error_message = htmlspecialchars($_GET["errormsg"]);
echo 'Error: ' . $error_message;
?>
prints: "Error: error_here", if url = ...?errormsg=error_here
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
My PHP code dosnt seem to be working and I dont Know Why when Ever I Use this code it will Make The whole webpage appear white.I think the problem is Here Somewhereecho Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');Thanks In Advance
session_start();
if(!isset($_SESSION['user']) && empty($_SESSION['user'])) {
echo '<b>Log In</b>';
}
else {
echo Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');
echo '</br></b>';
echo '<b>Log Out</b>';
}
When your page goes white, it usually means you have a fatal error in your code and need to check your logs, or turn on error_reporting.
In this case you're missing quotes, have the concatenation a bit messed up, and appear to be missing a function call (probably htmlspecialchars).
Also, you're checking $_SESSION['user'] a few lines before in your code, are you sure you don't mean to echo that here instead of $_SESSION['username']?
I think you want to change that line to:
echo "Hello, " . htmlspecialchars($_SESSION['user'], ENT_QUOTES, 'UTF-8');
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
I am trying to create an CMS-system where I can add photos to my website, and for the photo-part, I want to have the ability to enter which programs I had used to create that photo.
I should be possible to add more than one program.
Right now I am saving the programs in the database like this:
In the programs-row:
photoshop illistrator indesign
Then at my website, it could be nice if the icons/logos of the used programs, could show up next to the photo.
So my question is how to do create a new div, what a new class, based on the words from the programs-row?
Fx:
photoshop illustrator indesign
Becomes to:
<div class="photoshop"></div>
<div class="illustrator"></div>
<div class="indesign"></div>
Hope you guys can help me with this problem :)
Thanks ;)
Use the explode function and a foreach loop to perform the string manipulation.
$programs = explode(" ", $data);
foreach($programs as $value) {
//Echo out the html - $value contains the program name
}
I leave it to you to figure out how to format the program name with the HTML that you need.
$fx = "photoshop illistrator indesign";
$words = explode(" ", $fx);
array_walk($words, function(&$n) {
echo '<div class="'.$n.'"></div>';
});