Jump to content

Archived

This topic is now archived and is closed to further replies.

renjikk

Getting extension of a file using one line code

Recommended Posts

I have to get the extension of an uploaded file using one line code in php.How can i get it by using substr()? Please help me.

Share this post


Link to post
Share on other sites
$s="file2.php";
$s2=substr($s,-3);
echo $s2;
[code]

Share this post


Link to post
Share on other sites

$s="file2.php";
$s2=substr($s,-3);
echo $s2;
[code]

 

this only works with an extension of that is 3 chars long...

how about .jpeg, .html etc. etc.

 

to be sure, look for the last "." in the file name with strrpos

<?php
$s = "myfile.extension";
$dotPos = strrpos($s, ".");
$fileExtension = substr($s, $dotPos+1);
$fileName = substr($s, 0, $dotPos);
?>

 

Share this post


Link to post
Share on other sites

I'm not sure why you need to gfet it done it one line, but the easiet way is to use pathinfo. eg;

 

$filedetails = pathinfo('/path/to/file.html');
echo $filedetails['extension'];

Share this post


Link to post
Share on other sites

Try this


$filename = 'filename.php';

$extension = end(explode('.' , $filename));

echo $extension;


 

 

Share this post


Link to post
Share on other sites

I think this is what your looking for

<?php
$uploaded_file_name = "mytest.avi";
list($filename, $extension) = split('\.', $uploaded_file_name);

echo $uploaded_file_name;
?><br />
<?
echo $extension;
?>
<br />
<?
echo $filename;
?>

Share this post


Link to post
Share on other sites

Why 'one line code'?

 

[edit]

 

Aww... man... you got me into gravedigging...

Share this post


Link to post
Share on other sites

you can simplify thestars code for 1 line =D

 

echo end(explode('.' , "filename.php"));

Share this post


Link to post
Share on other sites

Ok... everyone... this topic was startted in March, and the OP hasn't posted since.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.