For example this is $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] output:
example.com/foldername/subfolder/controller/index.php
I want to end up with example.com/foldername/subfolder/controller/
Thanks..
Check out http://php.net/manual/en/function.pathinfo.php
<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>
The output for that would be
/www/htdocs/inc
lib.inc.php
php
lib.inc
So, in short, you could use $path_parts['dirname'].
print_r( $_SERVER );
http://www.php.net/manual/en/reserved.variables.server.php
http://www.php.net/manual/en/language.constants.predefined.php
PATH_TRANSLATED or __DIR__
Lots of options =)
Add the two together to get
$url = "http://". $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Related
So, the problem is in this line
$imageString = file_get_contents($image_url);
with urls that have space character it doesn't work. But if I make
$imageString = file_get_contents(urlencode($image_url));
Nothing works.I keep receiving false in the variable.
the ulr is of the kind:
https://s3-eu-central-1.amazonaws.com/images/12/Screenshot from 2016-04-28 18 15:54:20.png
use this function
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts))
;
}
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar+bof/some+file.jpg") . "\n";
echo escapefile_url("http://example.com/foo/bar%20bof/some%20file.jpg") . "\n";
i'v faced the same problem and if you search about it you will see all the people tell you to use urlencode(), but No!! urlencode() wont work in this situation...
i used the #Akram Wahid answer and that work perfectly so i recommend it to use for file_get_contents().
and if you wonder what escapefile_url() does in #Akram Wahid answer here little explain for it:
Simply he take the url apart as array and then he use rawurlencode() to encode all parts that contain special characters without the main domain like (http://example.com).
so what the deference?!! here example uses urlencode() and escapefile_url() to clarify this
echo escapefile_url("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http://example.com/foo/bar%20bof/some%20file.jpg
echo urlencode("http://example.com/foo/bar bof/some file.jpg") . "<br>";
// http%3A%2F%2Fexample.com%2Ffoo%2Fbar+bof%2Fsome+file.jpg
If you want to apply #Akram Wahid's solution to URLs that may also contain GET arguments then an updated version would be this:
function escapefile_url($url){
$parts = parse_url($url);
$path_parts = array_map('rawurldecode', explode('/', $parts['path']));
return
$parts['scheme'] . '://' .
$parts['host'] .
implode('/', array_map('rawurlencode', $path_parts)) .
(isset($parts['query']) ? '?'.rawurldecode($parts['query']) : '')
;
}
I have an URL in following format
www.domain.com/image.php?id=123&idlocation=987&number=01
It has image that has to be downloaded I use following function to download
$codigo = file_get_contents($url);
So now How I can get the exteion of the image file using $codigo
Thanks in advance
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
kindly refer this link to get file info
http://php.net/manual/en/function.pathinfo.php
I have this url which is generated automatically.
uploads/1/0/1/7/10178123/4320885_orig.png
I need to get the name and the extension of the image from this url which in this case is 4320885_orig.png
i am not being able to write a regex to find the name and extension.
I recommend you to check php's Basename function instead of using regex
echo basename("uploads/1/0/1/7/10178123/4320885_orig.png");
//result: 4320885_orig.png
Don't use regex, use pathinfo...
echo pathinfo('uploads/1/0/1/7/10178123/4320885_orig.png', PATHINFO_BASENAME);
Try pathinfo():
$path = 'uploads/1/0/1/7/10178123/4320885_orig.png';
echo pathinfo($path, PATHINFO_BASENAME); //png
You can get other components of the path as well:
$path_parts = pathinfo($path);
echo $path_parts['extension'], "\n"; //png
echo $path_parts['dirname'], "\n"; //uploads/1/0/1/7/10178123
echo $path_parts['basename'], "\n"; //4320885_orig.png
echo $path_parts['filename'], "\n"; //4320885_orig
See the PHP documentation on pathinfo() for more information.
I want to retireve page url using php but I would like some parts removed
<?php print("http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); ?>
Example: http://url.com/questions/page/112/
Result: http://url.com/page/112/
I would like to remove questions/ within the url. How would I do this?
$url="http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url=str_replace('/questions','',$url);
echo $url;
You'll want to use mod_rewrite, a module available in apache. This will be managed by an .htaccess file within your web directory. AddedBytes has a nice tutorial for beginners on url-rewriting.
check this site for detail
I would use php's explode function to split the example up into an array separated by "/"s, then loop through the array and where the array value = questions, unset or remove it from the array.
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Here's an example.
If you simply want to remove it as a string, you can use
$url = str_replace('/questions', '', $_SERVER["REQUEST_URI"]);
If you then want to redirect user to that page, you need to send a header (before any output):
header('Location: http://' . $_SERVER["HTTP_HOST"] . $url);
exit;
try this;
$str = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$parts = explode("/",$str);
$tmp = array();
for($i = 0; $i<count($parts)-2;$i++){
$tmp[$i] = $parts[$i];
}
$output = implode("/",$tmp);
You can use something like this
$sentence = str_replace('questions/', '', 'http://url.com/questions/page/112/');
//This splits the uri into an array
$uri = explode("/",$_SERVER["REQUEST_URI"]);
//Then Remove the first part of the uri (ie questions)
$first_uri = array_shift($uri);
//Recreate the string from the array
$uri = implode("/", $uri);
//Print like in your example
print("http://" . $_SERVER["HTTP_HOST"] . $uri);
//You can also access the remove string (questions) in the $first_uri variable
print($first_uri); //returns questions
$url='http://'.$_SERVER['HTTP_HOST'].preg_replace('/^\/questions/i','',$_SERVER['REQUEST_URI']);
echo $url;
I have a image path like this..
2012/12/14/example.jpg
Now I would like to get the path except image name..
I mean I need like this
2012/12/14/
Can anyone help me? Thanks
I guess this is what you need
<?php
$path_parts = pathinfo('2012/12/14/example.jpg');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n";
?>
output will be..
/www/htdocs/inc
example.jpg
jpg
example