This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is PHP not replacing the variable in string?
I have been trying to execute this line echo exec('hi.exe $file',$results,$status); from Php. where the value assigned to $file is the filename hi.txt (i.e. $file = hi.txt).
But each time i try to run my code with the same line, its showing error as $file file not found where as if i run the same hi.exe hi.txt in a command prompt its working.
And also if i try to run the same line with the filename instead of a variable from php i.e.exec('hi.exe hi.txt',$results,$status), the browser keeps executing for long time without giving the output.
Please someone tell me where i am going wrong!
You are using single quotes, instead of double quotes. Change echo exec('hi.exe $file',$results,$status); to:
echo exec("hi.exe $file",$results,$status);
or use a dot, like this:
echo exec('hi.exe '.$file,$results,$status);
In PHP, using single quotes won't turn $file into hi.txt; it just stays as the literal string, "$file". Use double quotes or dot concatenation to actually expand $file into hi.txt
Single quotes don't expand variables. You probably mean:
echo exec("hi.exe $file",$results,$status);
Related
This question already has answers here:
How do I escape an exclamation mark in bash?
(4 answers)
Closed 2 years ago.
I want to pass a string to a php script which contains ! character. Like this
php cfg.php --name=smtppass --set="MYW!ORD"
But I get this error
bash: !ORD: event not found
On the other hand, if I pass "MYW\!ORD", I see this string is set when I query the file
smtppass MYW\!ORD
Any idea to fix that?
You could pass it like this:
php cfg.php --name=smtppass --set="MYW"'!'"ORD"
or a more lazy way would be to use single quotes.
php cfg.php --name=smtppass --set='MYW!ORD'
This happens because of Bash's history expansion, which is sometimes very dangerous, you could turn this feature off by typing set +H.
This question already has answers here:
How do I execute PHP that is stored in a MySQL database?
(7 answers)
Closed 4 years ago.
EDIT: This question has been edited from the original
I have a string in a database with HTML and PHP variable's inside. Some of the HTML has double quotes as if I try to use single quotes the database escapes it by adding a quote in front of it like so: ' '.
I want to query the string and assign it to variable $x. And then use eval("\$x = \"$x\";"); to parse the PHP variable, but it seems the double quote is ruining the eval(), and the variables are not parsing.
Is there a way to allow PHP to read the variable?
I am aware, but anyone reading this should also be aware that using eval() can be very dangerous!
Any help would be greatly appreciated!
If your SQL string looks like this: myVar then php:
$myVar = 'hello!';
echo $$var;
If your SQL string looks like this: 3 + 5 then php:
eval($var);
In first option we use Variable Variables
In second option we use eval to evaluate code in string.
This question already has answers here:
Can I echo a variable with single quotes?
(11 answers)
Closed 9 years ago.
Im having some troubles unlinking files from a directory. I'll try to explain it as good as i can.
Im trying to delete a file from a directory, first i get the file name from my database, where is stored in field "avatar".
these are my involved vars:
$avatar1=mysqli_query($con,"Select avatar from users where user='$_SESSION[Username]'");
$avatar2=mysqli_fetch_array($avatar1);
$avatardirectory = $avatar2['avatar']; //(missunderstanding name, its actually a file).
So far, when i print $avatardirectory when my user is hodor, it shows "hodor.png"
Ok, here comes the nasty part, i try to go with this:
unlink('/var/www/html/test/img-gallery/$avatardirectory'); //This wont work.
Then I just do the same exact thing but with filename:
unlink('/var/www/html/prueba/img-gallery/hodor.png'); //This actually works.
And now I'm totally lost.
Variables arn't expanded inside single quote strings.
You might want to see the difference in
echo '/var/www/html/test/img-gallery/$avatardirectory'
and
echo "/var/www/html/test/img-gallery/$avatardirectory"
Note that unlink() removes files, not directories. use rmdir() to remove (an empty) directory. (though it sounds like your $avatardirectory holds the name of a file, not a directory(?) )
This question already has an answer here:
How to turn off magic quotes in PHP configuration file? I am using XAMPP
(1 answer)
Closed 9 years ago.
My PHP page is accepting a parameter in the URL. This parameter is being assigned to a variable as follows:
$msg = $_REQUEST["msg"];
When the HTTP request is sent to the website, the parameter is sent as "hello'", but when it gets to the PHP variable above it becomes "hello\'".
Why is the backslash being inserted and what is inserting it? Is it the web server? How can I prevent this happening?
Magic Quotes is running on you server. You should use stripslashes($text) function:
if(get_magic_quotes_gpc())
$msg = stripslashes($_REQUEST["msg"]);
else $msg = $_REQUEST["msg"];
It is being appended cause your single quote sign is a part of the string. If it would not be escaped - that's the meaning of the backslash - it might be that your string definition is terminated too soon.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Can't decode JSON string in php
I'm at my wits end here, and I can't figure it out.
My code was working correctly locally (using xamp) but now it won't work.
When I run this code:
echo "passed in parameter" . $_POST["jsoned"];
$unjasoned = json_decode("[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]");
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);
I get:
passed in parameter[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]
type
array
Amount
23
Which is exactly what i want
However the problem happens when I use the passed variable in $_POST["jsoned"] which is you see in the result above is obviously exactly the same as what im manually inserting here.
so if i do this instead (same exact input):
echo "passed in parameter" . $_POST["jsoned"];
$unjasoned = json_decode($_POST["jsoned"]);
die("\ntype\n\t". gettype($unjasoned) . "\n\n\nAmount\n\t" . $unjasoned[0]);
I get:
passed in parameter[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]
type
NULL
Amount
so...... WHAT THE HELL IS HAPPENING?! PLEASE if you have any hints share them with me, ill be eternally thankful.
ps. my server runs php version 5.2.13
answered already here Can't decode JSON string in php
The string in the post has \" instead of ".
When you write the string yourself as a literal you have to write \" because you're inside double quotes, but in the resulting string you'll only get a ".
Try this debug to see the difference:
echo $_POST["jsoned"], PHP_EOL;
echo "[\"23\",[],[[\"a#a.a\",\"2011-01-08\"]]]";