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);
Related
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.
I have a phpbb external script which outputs a relative url
(like ./forum/viewtopic.php?f=xx&t=xx),
but for making an rss feed I need the absolute url
(http://example.com/forum/viewtopic.php?f=xx&t=xx) to use.
My question is: How could I make this?
I suppose it should just be a string function, to replace the first dot and then concatenate the http://example.com before it, but I don't have enough php skills to do that.
Simply replace your ./ with the hostname.
$url = str_replace('./', 'http://example.com/', $url);
This is my url:
ftp://dynamic_text:user_password#my-ftp-domain.com/so-on/param
My regex will turn it into this:
ftp://*****:*****#my-ftp-domain.com/so-on/param
Note that the url can start with either ftp or http.
regex:
My regex below will always return ftp regardless if my url started with http.
preg_replace('#(ftp|http)://(.*:.*)\##', 'ftp://****:****#', $url);
Now my question is: how can I modify my code, so that it will dynamically return ftp or http depending on how my url started.
I read about Named Groups, but I wasn't able to solve it.
Just change the ftp part in your replacement to $1 to get the value of the first group, e.g.
preg_replace('#(ftp|http)://(.*:.*)\##', '$1://****:****#', $url);
//^^
I have the following url. http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png
Everything in the url can change except the userfiles part and the last underscore. Basically I want to get the part of the url which is userfiles/dynamic/images/whatever_dollar_ What is a good way to do this. I'm open or both JavaScript or php.
Use parse_url in PHP to split an url in its various parts. Get the path part that is returned. It contains the path without the domain and the query string.
After that use strrpos to find the last occurrance of the _ within the path.
With substr you can copy the first part of the path (up until the found _) and you're done.
You could, with JavaScript, try:
var string = "http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png";
var newString = string.substring(string.indexOf('userfiles'),string.lastIndexOf('_'));
alert(newString); // returns: "userfiles/dynamic/images/whatever_dollar" (Without quotes).
JS Fiddle demo.
References:
substring().
indexOf().
lastIndexOf().
Assuming your string is stored in $s, simply:
echo preg_replace('/.*(userfiles.*_).*/', '$1', $s);
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).