I have downloaded some PHP code because I want to modify it and use it for my project.
I have this line of code:
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
I know the $_SERVER['PHP_SELF'] it is a superglobal variable that returns the file name of the current running script.
But I do not know why rtrim is used!
Can someone shortly explain to me?
From the PHP doc:
rtrim — Strip whitespace (or other characters) from the end of a
string
To explain more :
_SERVER['PHP_SELF']:
will return The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php
And the dirname will Returns a parent directory's path like src/foo/bar.php.
To ensure that there is no special caracter inside , the rtrim will strip whitespace (or other characters) from the end of a string.
Hope this help you.
Related
Every example I find for file_get_contents and FILE_USE_INCLUDE_PATH show the file name as a text string inside quotes. I see that the dot concatenation is used (I'm presuming to concatenate the PATH and the actual file name) and when I follow that method, my code works. However, I need to create a variable for the file name and when I do PHP stops.
I have set_include_path( PATH ); and I see the desired folder WITHOUT a forward slash (/).
I have tried both with and without a forward slash in front of the file name when I assign it to the variable ($var_ame = "file.dat";) and ($var_ame = "/file.dat";) with no difference.
This works: $expected_var = file_get_contents('./file.dat', FILE_USE_INCLUDE_PATH);
This DOES NOT: $expected_var = file_get_contents($var_name, FILE_USE_INCLUDE_PATH);
I have tried a dot in front of the variable knowing that I need to concatenate somehow but that stops PHP as well.
What am I missing here?
The dot in file_get_contents('./file.dat', FILE_USE_INCLUDE_PATH); is not a string concatenation operator, it's an actual dot inside the string. If you want that same string in a variable, you want to include that dot: $filename = './file.dat';
Meanwhile, the fact that the dot is there means that FILE_USE_INCLUDE_PATH is irrelevant, because . is the special name for "the current directory", so './file.date' means "a file called 'file.dat' in the current directory", and won't search any other directory.
What "the current directory" means in a running PHP script is not always obvious, so as a general good practice, I would suggest not relying on it, or on the configured path, and always using an absolute path. The magic constant __DIR__ expands to whatever directory the current PHP file is placed in, which can be useful for constructing such a path.
I am writing a PHP function in Drupal to detect duplicate file uploads and attempting to compare the uploaded filename to previously uploaded files.
I have example files of:
trees-nature_0.jpg
trees-nature_1.jpg
trees-nature0.jpg
trees-nature.jpg
I am trying to match all of them all using the following code:
file_scan_directory('image/uploads', "/trees-nature[*]?.jpg/");
However, all I get back is trees-nature.jpg.
I would appreciate some correction.
Your regex is not correct. use:
file_scan_directory('image/uploads', '/trees-nature.*?\.jpg/');
You can use the following:
file_scan_directory('image/uploads', '/trees-nature(.*?)\.jpg/');
Correction:
[ ] cannot be used as parentheses.. it has special meaning in regex
* is not wildcard in regex.. you have to use .*
. also has special meaning here (any character) you need to escape it
Does PHP have a native function that returns the full URL of a file you declare with a relative path? I need to get: "http://www.domain.com/projects/test/img/share.jpg" from "img/share.jpg" So far I've tried the following:
realpath('img/share.jpg');
// Returns "/home/user/www.domain.com/projects/test/img/share.jpg"
I also tried:
dirname(__FILE__)
// Returns "/home/user/www.domain.com/projects/test"
And this answer states that the following can be tampered with client-side:
"http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'img/share.jpg"
plus, my path will vary depending on whether I'm accessing from /test/index.php or just test/ without index.php and I don't want to hard-code whether it's http or https.
Does anybody have a solution for this? I'll be sending these files to another person who will upload to their server, so the folder structure will not match "/home/user/www.domain.com/"
echo preg_replace(preg_quote($_SERVER['DOCUMENT_ROOT']), 'http://www.example.com/', realpath('img/share.jpg'), 1);
Docs: preg_replace and preg_quote.
The arguments of preg_replace:
preg_quote($_SERVER['DOCUMENT_ROOT']) - Takes the document root (e.g., /home/user/www.domain.com/) and makes it a regular expression for use with preg_replace.
'http://www.example.com/' - the string to replace the regex match with.
realpath('img/share.jpg') - the string for the file path including the document root.
1 - the number of times to replace regex matches.
How about
echo preg_replace("'". preg_quote($_SERVER['DOCUMENT_ROOT']) ."'",'http://www.example.com/', realpath('img/share.jpg'), 1);
I am trying to remove any trailing slash from DOCUMENT_ROOT
rtrim($_SERVER['DOCUMENT_ROOT'], '/\\')
But its adding %5C after using rtrim:
E:%5Cwamp%5Cwww%5Ctestfolder
Also I replace all slash with DIRECTORY_SEPARATOR before using rtrim, but same thing happen.
Please tell me why its happening ?
The %5C is from URL encoding. The $_SERVER['DOCUMENT_ROOT'] value is not URL-encoded, so perhaps you are urlencoding the output when you send it to your browser?
rtrim only trims the end of the string, and your example output shows that no slash is suffixed to the string - it appears rtrim has done its job.
Please post your whole code for a more detailed look.
I am trying to understand this one line of code below:
str_replace('../', '', $route);
Basically it says replace '../' with nothing in $route
$route = 'information/information&information_id=4';
from the url 'index.php?route=information/information&information_id=4'
But there is no ../ in the $route variable. Is it some sort of regex? If yes, what does it exactly do. Thanks guys.
You are correct in thinking that it replaces "../" with an empty string. It is not regex. There is no occurence of it in your example, but there could be.
It might be used for some sort of security to prevent you from going back up the directory structure from the document root.
If there's no ../ in the string, this will replace nothing. It's not a regex (see preg_replace() for that. It's just precaution against someone trying to pass invalid path (starting with ../), which could potentially be an attempt of accessing files outside of webserver's document root (in other words, a hacking attempt).