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. Quote Link to comment 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] Quote Link to comment Share on other sites More sharing options...
dgoosens Posted March 24, 2009 Share Posted March 24, 2009 $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); ?> Quote Link to comment 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']; Quote Link to comment Share on other sites More sharing options...
cornetofreak Posted March 28, 2009 Share Posted March 28, 2009 use this $extension = strrchr($filename,"."); Quote Link to comment 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; Quote Link to comment 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; ?> Quote Link to comment 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... Quote Link to comment 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")); Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.