i have a script which us to read jpg images in a folder where this script file exist and displays in a gallery on browser it read file name by $filename i want to change little that it read file names but not its extension please tell me how can i do this
try using the explode method ... $newfilename = array(); $newfilename = explode(".",$filename); $filename = $newfilename[0]; try this and tell me how it works ..
You should use this method instead: $filename = '/www/path/to/picture/picture.jpg'; $fileParts = pathInfo($fileName); PHP: this method will give you access to specifics using a well defined array instead of an arbitrarily indexed array: echo $fileParts['dirname']; // /www/path/to/picture echo $fileParts['basename']; // picture.jpg echo $fileParts['filename']; // picture echo $fileParts['extension']; // jpg PHP: The reason you'd want to do it this way is to provide fault tolerance for random naming conventions. Lets say your file name is = 'Auth.class.php' If you explode on '.', the first element [0] will give you 'Auth', which is not the correct filename.
$file = 'image1.3242542.abcdefg.jpg'; //reading the image file name except the dot and last three letter echo substr($file,0,-4); PHP:
Lol, well in a real life situation he would probably accept every kind of image, even .jpeg's . . . pathInfo(); // is the best method to go with. PHP:
Okay here's your code. work in all file extension. $file = 'image1.3242542.abcdefg.jpeg'; echo substr($file,0,-(strlen(strrchr($file,'.')))); PHP:
But why calling 3 functions when all you need is one? pathinfo() and basename() will work just fine...
4th way i have my own last char of did not know about the php 1 ... but i like strrchr over pathinfo() cause path info does a lot internally ....
Agreed, pathinfo() was design to get file path and it's do more work internally to find the file path.