This question already has answers here:
How to get a file's extension in PHP?
(31 answers)
Closed 2 years ago.
I'm exploding on "." to get file format and name:
list($txt, $ext) = explode(".", $name);
The problem is that some files have names with dots.
How do I explote on the LAST "." so that I get $name=pic.n2 and $ext=jpg from: pic.n2.jpg?
Use pathinfo:
$pi = pathinfo($name);
$txt = $pi['filename'];
$ext = $pi['extension'];
$name = pathinfo($file, PATHINFO_FILENAME);
$ext = pathinfo($file, PATHINFO_EXTENSION);
http://www.php.net/pathinfo
use this
$array = explode(".", $name);
end($array); // move the internal pointer to the end of the array
$filetype = current($array);
thanks
Use Pathinfo or mime_content_type to get file type information
$filetype = pathinfo($file, PATHINFO_FILENAME);
$mimetype = mime_content_type($file);
Use PHP's pathinfo() function.
See more information here http://php.net/manual/en/function.pathinfo.php
$file_part = pathinfo('123.test.php');
Example:
echo $file_part['extension'];
echo $file_part['filename'];
Output:
php
123.test
<?php
$path = 'http://www.mytest.com/public/images/portfolio/i-vis/abc.y1.jpg';
echo $path."<br/>";
$name = basename($path);
$dir = dirname($path);
echo $name."<br/>";
echo $dir."<br/>";
$pi = pathinfo($path);
$txt = $pi['filename']."_trans";
$ext = $pi['extension'];
echo $dir."/".$txt.".".$ext;
?>
you can write your own function as
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
You might try something like this:
<?php
$file = 'a.cool.picture.jpg';
$ext = substr($file, strrpos($file, '.')+1, strlen($file)-strrpos($file, '.'));
$name = substr($file, 0, strrpos($file, '.'));
echo $name.'.'.$ext;
?>
The key functions are strrpos() which finds the last occurrence of a character (a "." in this case) and substr() which returns a sub string. You find the last "." in the file, and sub string it. Hope that helps.
It is better to use one of the solutions above, but there is also a solution using the explode function:
$filename = "some.file.name.ext";
list($ext, $name) = explode(".", strrev($filename), 2);
$name = strrev($name);
$ext = strrev($ext);
What this solution does is the following:
1. reverse string, so it will look like: txe.eman.elif.emos
2. explode it, you will get something like: $ext = "txe", $name = "eman.elif.emos"
3. reverse each of the variables to get the correct results
Related
I'm having a problem in my code, I am trying to append a number to a filename if filename already exists. It goes something like this
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
while (file_exists($location.$fileName)) {
$i++;
}
$fileName= $i.$fileName;
$name = $fileName;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);
if ($moveResult != true) {
#unlink($fileTmpLoc);
header('location: ' . URL . '?page=0&sort=name&type=desc&folder=uploads/&message=uploaderror');
}
Unfortunately for some reason $i wont increase its value by 1 every time it loops, instead it adds to the filename this way 1234filename.jpg my file name variable is after the loop and i cant understand why this is accruing. I am expecting to get ($i)filename.jpg a single number
AFTER RESTARTING MY LOCALSERVER IT STARTED WORKING WITH THE CODE PROVIDED BELOW DUUUH
You need to use the actual filename when you concat the number to it and not the one you already added a number to.
// not sure why you are splitting the filname up here
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
$fn = $fileName;
while (file_exists($location.$fn)) {
$i++;
// add number to actual filename
$fn = $i.$fileName;
}
$name = $fn;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);
im trying to remove the file extension from each file name in a loop so ball.jpg can be echoed as ball, but it isnt working for me
I have this code
$files = array();
foreach($src_files as $file)
{
$ext = strrchr($file, '.');
if(in_array($ext, $extensions))
{
array_push( $files, $file);
$thumb = $src_folder.'/'.$file;
$fileName = basename($file);
$place = preg_replace("/\.[^.]+$/", "", $fileName);
}
}
Try this:
$src_files = array('/tmp/path/file1.txt', '/tmp/path/file2.txt.php', '/tmp/not.ext');
$extensions = array('.txt', '.php');
$files = array();
foreach ($src_files as $file)
{
$ext = strrchr($file, '.');
var_dump($ext);
if (in_array($ext, $extensions))
{
array_push($files, $file);
//$thumb = $src_folder.'/'.$file;
$pathInfo = pathinfo($file);
$fileName = $pathInfo['basename'];
$place = $pathInfo['filename'];
var_dump($pathInfo);
}
}
If you just want the filename, without extension, use pathinfo($fileName, PATHINFO_FILENAME);. See here for more information.
If you don't want to use pathinfo(), you can also use string manipulation techniques:
$place = substr($fileName, 0 , (strrpos($fileName, ".")));
strrpos() is like strpos(), but searches for a character starting from the end of the string and working backwards.
I want to concat a single quote at the end of the string
How can i do this thing in php?
$file = $this->form->getValue('doc');
$filename = $file->getOriginalName();
$file_name=$fn.'_'.md5($fiename);
$extension = $file->getExtension($file->getOriginalExtension());
try this:
$file_name=$fn.'_'.md5($fiename).",";
in php . is used for concat.
Try like this
$file = $this->form->getValue('doc');
$filename = $file->getOriginalName();
$file_name=$fn.'_'.md5($filename); //There is no $fiename, so i changed it to $filename
$file_name=$file_name."'";//Concatenating here.
$extension = $file->getExtension($file->getOriginalExtension());
Try this
$file = $this->form->getValue('doc');
$filename = $file->getOriginalName();
$file_name=$fn.'_'.md5($fiename)."'";
$extension = $file->getExtension($file->getOriginalExtension());
I need your help with a RegEx in PHP
I have something like:
vacation.jpg and I am looking for a RegEx which extracts me only the 'vacation' of the filename.
Can someone help me?
Don't use a regex for this - use basename:
$fileName = basename($fullname, ".jpg");
You can use pathinfo instead of Regex.
$file = 'vacation.jpg';
$path_parts = pathinfo($file);
$filename = $path_parts['filename'];
echo $filename;
And if you really need regex, this one will do it:
$success = preg_match('~([\w\d-_]+)\.[\w\d]{1,4}~i', $original_string, $matches);
Inside matches you will have first part of file name.
Better answers have already been provided, but here's another alternative!
$fileName = "myfile.jpg";
$name = str_replace(substr($fileName, strpos($fileName,".")), "", $fileName);
You don't need regex for this.
Approach 1:
$str = 'vacation.jpg';
$parts = explode('.', basename($str));
if (count($parts) > 1) array_pop($parts);
$filename = implode('.', $parts);
Approach 2 (better, use pathinfo()):
$str = 'vacation.jpg';
$filename = pathinfo($str, PATHINFO_FILENAME);
This question already has answers here:
How to get a file's extension in PHP?
(31 answers)
Closed 2 years ago.
Can anyone help me change this script to use preg_split (recommended substitute by php.net) instead of split which is not used anymore. This function gets the file extension of any uploaded file in the variable $filename.
function findExtension ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
You should just use pathinfo instead:
$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
Why don't u use this function : http://www.php.net/manual/fr/function.finfo-file.php or this one : http://fr2.php.net/manual/fr/function.pathinfo.php
you can also use explode
function findExtension ($filename)
{
$filename = strtolower($filename) ;
$exts = explode(".", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
Instead of split you can just use explode. As you just want the extension, there's no reason to split by /, just split by the dot and get the last element with array_pop.
I prefer a function David Walsh posted, that uses the "strrchr" function to get the last occurrence of "." in a string.
function get_file_extension($file_name)
{
return substr(strrchr($file_name,'.'),1);
}
If the file extension is the only part you want:
function GetExt($filename) {
return (($pos = strrpos($filename, '.')) !== false ? substr($filename, $pos+1) : '');
}
Perhaps something along these lines?
$string = "some/path_to_a_file.txt";
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
My code will give file extension, removing query strings. pathinfo also return extension with string. so use my code if you want to know the exact file name:
$filename = 'http://doamin/js.jquery.min.js?v1.1.11';
preg_replace('/\?.*/', '', substr(strrchr($filename, '.'), 1));
// output: js