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
Related
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
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'];
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.
Hi I am trying to get images to load into a page using the file names from an array,
This is what I have so far
<?php
$i=0;
$img=array("1.png","2.png","3.png","4.png");
while ($i<count($img))
{
echo "<img class='loadin' alt='imgg' src=" . "'http://www/images/" . $img[i] . "'" . "/" . ">" . "<br/>";
$i++;
}
?>
It seems to ignore the file name and just enters:
http://www/images/
as the source and ignores the file name from the array
Any Help would be great Thanks
Mikey
You forgot the dollar sign with your $i variable: $img[$i]
EDIT:
(btw. using a foreach-loop would be easier...)
foreach($img AS $filename) {
echo "<img class='loadin' alt='imgg' src='http://www/images/" . $filename . "'/><br/>";
}
E.g:
folder name:
myFonlder
files in myFolder
myFolder.01.mkv
myFolder.02.mkv
myFolder.03.avi
myFolder.04.mts
...
// each file's extension may be different.
So,how can I extract the extension of each file?
Thank you very much!!
[update]
my own solution; want to know is it fast enough!?
foreach (glob("d:\\myFolder\\*.*") as $filename) {
//echo "$filename size " . filesize($filename) . "\n";
$path_parts = pathinfo($filename);
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
}
<?php
foreach (new DirectoryIterator('../moodle') as $fileInfo) {
if($fileInfo->isDot()) continue;
echo $fileInfo->getFilename() . "<br>\n";
}
?>
And use http://www.php.net/manual/en/function.pathinfo.php on filename
This is the quick and easy solution on windows.
exec("dir d:\directory_name /b" ,$output); // in Linux dir will change to ls
foreach($output as $file_name){
$file_parts = explode(".",$file_name);
echo "File Name : ".$file_parts[0]."\n";
echo "File Extension : ".$file_parts[1]."\n\n";
}
Enjoy..!!