i have problem with file() in php. The function can't read a file start with ! or $ like !textfile.txt or $textfile.txt, i try with fopen() and it happen as a same way. I have no idea how to solve this. I hope someone can help
Appreciate any help.
The filename "$textfile.txt" will not work as expected because variable interpolation happens in double quotes as a result value of variable $textfile will be appended with .txt and the result will be used as filename. If $textfile is undefined (which mostly is the case), .txt will be used as the filename.
To fix this use single quotes around the filename as '$textfile.txt' or if you have to use double quotes, escape the $ as: "\$textfile.txt"
But I see no problem with "!textfile.txt"
echo file_get_contents("\$test.txt");
Works.
You need to escape special characters or use single quotes.
Related
I am trying to get the file to upload an image or audio file to the following directory
$dir='sites/default/documents/"$patientID"/' but I don't know how to use the exact syntax with the $ sign inside a path.
I used the get method to get the patientID from the controller but still nothing is working
Single quotes do not interpolate values, even if there are double quotes inside them. The initial string must be a double quote string, in order for any interpolation to occur inside of it.
If there's any doubt about the complexity of the variable name, use braces to encapsulate the interpolation. $dir="sites/default/documents/{$patientID}/";
Use this instead : $dir="sites/default/documents/$patientID/";
In your case the variable is considered as a text.
I am new to Laravel and I am having this question.
I tried out this line of code and it works fine: return redirect("/cards/{$note->id}");
But when ever I try to use the single quotes, it does not work: return redirect('/cards/{$note->id}');
How can I solve this problem ?
What you are doing first is called variable interpolation or string interpolation. You can read more about it here, on PHP docs and here, on Wiki.
It's a feature in PHP that allows you to pass a string and have variables/placeholders inside interpreted.
In your second example you are using single quotes, which does not provide this feature, so you will have to break it up and add the variable manually to the string:
return redirect('/cards/' . $note->id);
If you are interested in a more elaborate explanation and the performance behind it then you can read more on this answer here by Blizz
He concludes that:
Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.
However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.
You should use "/cards/{$note->id}" or '/cards/'.$note->id
The most important feature of double-quoted strings is the fact that variable names will be expanded.
When a string is specified in double quotes or with heredoc, variables are parsed within it.
From PHP documentation
Use it like that:
return redirect('/cards/'. $note->id);
With either single or double quotes
Hello there :) I have a script in PHP that creates a file (with the function "file_put_contents()") , and it will put the contents of a PHP file within the file it just created. Within the PHP file, there are double quotations, so as i try to implement the code that i want to put into this newly made PHP file, it has double quotations, and the way how file_put_contents works is the second part of it uses double quotations as well.
To put it into perspective, this is how it goes: file_put_contents('file.php',"code with "" in it")
so as you can see, the double quotations get in the way of the PHP files double quotes.
My question is, how do i get the text within the quotes to not parse?
Thanks
use \ more info
file_put_contents('file.php',"code with \"\" in it")
or use ' to quote second param
file_put_contents('file.php','code with "" in it')
Wrap the string which contains double quotes in a string literal defined using single quotes.
file_put_contents('file.php','code with "" in it');
"Escaping" is what you're looking for. As always, the manual holds all your answers:
https://secure.php.net/manual/en/language.types.string.php
You just need to escape the double quotes like this : file_put_contents('file.php',"code with \"\" in it")
For more information : http://php.net/manual/en/language.types.string.php
Try using addslashes("codewith dowble quotes") to over come this issue
I'm trying to use str_replace to correct a filepath as shown below:
$a="F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449";
$a=str_replace('\\','/', $a);
echo $a;
returns:
F:
mpp/htdocs/yii/get_smart/Music/mix/Englishs6464449
Can someone please tell me what I'm doing wrong?
My PHP version is 5.3.8
Use single quote for define $a
$a='F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449';
the problem is not str_replace but the string defined within double quotes. The backslashes escape the x and other character after it.
This is happening because your string is in double quotes, so the \x is being parsed as a character.
Actually, it's trying to read \xam as a character. Docs: http://php.net/manual/en/regexp.reference.escape.php
Put your string in single quotes (or escape the slash before the x).
Your problem is that the first string has some escaped sequences. For example \xam has a meaning in php. It looks like \16 might also mean something. You should echo $a before you do the str_replace and see what you get.
I'm trying to delete a file this way:
define('DESTINATION_FOLDER','c:\temp\\');
unlink(DESTINATION_FOLDER.$dest_filename);
And $dest_filename is something like this: 2jfioj23488hgh83hr.zip
But this doesn't work. When I put the path in a variable and echo it, it prints:
c:\temp\2jfioj23488hgh83hr.zip
Then I copy/paste it in unlink():
unlink('c:\temp\2jfioj23488hgh83hr.zip')
And it works. I think the problem is with the double backslashes. But how should I define the path without using the double backslashes? I don't know why it doesn't work. The line just above this code is:
copy((DESTINATION_FOLDER.$dest_filename),($extract.'\\'.$dest_filename));
And it works fine, but unlink doesn't work with the same syntax.
Putting my comment as an answer to the question
Try using forward slashes instead of backslash :)
If for some reason you must use backslashes, then you need to use c:\\ instead of c:\
define('DESTINATION_FOLDER','c:\\temp\\');