Get the file extension (basename?) - php

If I have a code like this:
$file = basename($filename);
How do I get the file extension of $file? The variable $file could contain any kind of file, like index.php or test.jpeg.

Use the pathinfo() function:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n";
or simply:
echo pathinfo($file, PATHINFO_EXTENSION);
You can of course look for the last "." in the filename and get everything after (relatively easy) but why reinvent the wheel?

pathinfo($filename, PATHINFO_EXTENSION);

Related

how to get foldername and filename using regex

i have output from string
foldername/subfolder/filename.jpg
how to get only foldername/subfolder/ and only filename.jpg
$filepath = 'foldername/subfolder/filename.jpg';
$folder= (preg_match('~[^A-Za-z0-9_\./\]~', $filepath));
echo 'folder:' .'<br>' .$folder;
$filename =(preg_match('....', $filepath));
echo 'fileneme' .'<br>' .$filename;
output
folder:
foldername/subfolder/
filename:
filename.jpg
thank you
Try this
$filepath = 'foldername/subfolder/filename.jpg';
// using regex
$parts = preg_split('~/(?=[^/]*$)~', $filepath );
echo " folder::".$parts[0]." AND fileneme:". $parts[1];
DEMO
Or use pathinfo
$filepath = 'foldername/subfolder/filename.jpg';
$path_parts = pathinfo($filepath);
echo "dirname : ".$path_parts['dirname']."\n";
echo "basename : ".$path_parts['basename']."\n";
echo "extension : ".$path_parts['extension']."\n";
echo "filename : ".$path_parts['filename']."\n";
DEMO

exploding string to remove path

My script is returning the following path.
/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php
I want to remove the rest and end up with the file name.
I know I have to explode the string, I just don't really know how to go about it.
use basename
echo basename("/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php");
//api.php
OR pathinfo
$path_parts = pathinfo('/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php');
echo $path_parts['basename']; // since PHP 5.2.0
//api.php
<?php
$path = "/home/vol14_2/project.com/b22_16126933/test.project.com/htdocs/php/api.php";
$file = basename($path); // $file is set to "api.php"
$file = basename($path, ".php"); // $file is set to "api"
?>

Remove image extension, rename and restore

Say the image is called:gecko.jpg
Can I first remove ".jpg" and add "-100x100" after "gecko", and then put the extension back, so it would be "gecko-100x100.jpg"?
use pathinfo
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
$new = $path_parts['filename'] . '-100x100.' .$path_parts['extension'];
Yes, quite simply with PHP's string functions in conjunction with basename()
$base = basename($filename, ".jpg");
echo $base . "-100x100" . ".jpg";
Or to do it with any filetype using strrpos() to locate the extension by finding the last .
// Use strrpos() & substr() to get the file extension
$ext = substr($filename, strrpos($filename, "."));
// Then stitch it together with the new string and file's basename
$newfilename = basename($filename, $ext) . "-100x100" . $ext;
--
// Some examples in action...
$filename = "somefile.jpg";
$ext = substr($filename, strrpos($filename, "."));
$newfilename = basename($filename, $ext) . "-100x100" . $ext;
echo $newfilename;
// outputs somefile-100x100.jpg
// Same thing with a .gif
$filename = "somefile.gif";
// outputs somefile-100x100.gif

PHP: Problem with filesize

I need a little help here:
I get a file from an HTML upload form. And I have a "target" filename in $File.
When I do this:
copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($_FILES['binfile']['tmp_name']);
echo '<hr>' . filesize($File);
Everything works fine. I get the same number twice.
However when I delete the first call of filesize(), I get "0" (zero).
copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($File);
Any suggestions? What am I doing wrong? Why do I need to get the filesize of the "original" file before I can get the size of the copy?
(That's actually what it is: I need to call the filesize() for the original file. Neither sleep() nor calling filesize() of another file helps.)
System:
Apache 2.0
PHP 5.2.6
Debian Linux (Lenny)
How big is this file? You are doing a copy and then stating the file straight away. Could this be the problem?
Does the builtin move_uploaded_file() function give the same behavior?
Try this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
$filesize = $_FILES['binfile']['size'];
echo '<hr>' . $filesize;
How about this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
while (empty(filesize($File)))
sleep(2);
echo '<hr>' . filesize($File);
OR try this:
copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
while (!file_exists($File))
sleep(2);
echo '<hr>' . filesize($File);

hiding file extension it self

I looking for your help please
I use simple php code like:
$file = $_SERVER["SCRIPT_NAME"];
//echo $file;
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
echo $pfile;
than output $pfile e.g. fileforme.php
but that i want is output of $pfile become fileforme
because i want use it to:
$txt['fl']= $pfile ;
how to do? or there are any another better way?
You can use pathinfo() and its PATHINFO_FILENAME flag (only available for php 5.2+)
e.g.
$file = $_SERVER["SCRIPT_NAME"];
echo 'file: ', $file, "\n";
echo 'PATHINFO_FILENAME: ', pathinfo($file, PATHINFO_FILENAME);
prints (on my machine)
file: C:\Dokumente und Einstellungen\Volker\Desktop\test.php
PATHINFO_FILENAME: test
See basename in the PHP Manual:
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
or use pathinfo if you do not know the extension:
$path_parts = pathinfo('/www/htdocs/index.html');
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
Basically you're looking for the filename without the extension:
$filename = basename($_SERVER["SCRIPT_NAME"], ".php");
Note that this answer is specific for the php extension.
$pfile_info = pathinfo($pfile);
$ext = $pfile_info['extension'];
See pathinfo() for further information.

Categories