renjikk Posted March 23, 2009 Share Posted March 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/ Share on other sites More sharing options...
suma237 Posted March 24, 2009 Share Posted March 24, 2009 $s="file2.php"; $s2=substr($s,-3); echo $s2; [code] Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-792412 Share on other sites More sharing options...
dgoosens Posted March 24, 2009 Share Posted March 24, 2009 Quote $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); ?> Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-792418 Share on other sites More sharing options...
trq Posted March 24, 2009 Share Posted March 24, 2009 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']; Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-792431 Share on other sites More sharing options...
cornetofreak Posted March 28, 2009 Share Posted March 28, 2009 use this $extension = strrchr($filename,"."); Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-795963 Share on other sites More sharing options...
thestars Posted May 11, 2009 Share Posted May 11, 2009 Try this $filename = 'filename.php'; $extension = end(explode('.' , $filename)); echo $extension; Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-831449 Share on other sites More sharing options...
phpflavor Posted May 17, 2009 Share Posted May 17, 2009 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; ?> Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-835933 Share on other sites More sharing options...
Mchl Posted May 17, 2009 Share Posted May 17, 2009 Why 'one line code'? [edit] Aww... man... you got me into gravedigging... Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-835935 Share on other sites More sharing options...
BK87 Posted May 18, 2009 Share Posted May 18, 2009 you can simplify thestars code for 1 line =D echo end(explode('.' , "filename.php")); Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-836166 Share on other sites More sharing options...
Mchl Posted May 18, 2009 Share Posted May 18, 2009 Ok... everyone... this topic was startted in March, and the OP hasn't posted since. Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-836227 Share on other sites More sharing options...
phpflavor Posted May 18, 2009 Share Posted May 18, 2009 lol yep but info like this is awsome to have around Link to comment https://forums.phpfreaks.com/topic/150676-getting-extension-of-a-file-using-one-line-code/#findComment-836699 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.