Cep Posted November 7, 2006 Share Posted November 7, 2006 Hello,I am writing an upload function at the moment and I need to validate the filename as users will try to place full stops as part of dates for example in the filename and come up with names like "06.11.07-reports.doc".As I want to determine the file extension using this,[code=php:0]function get_extension($filename){ $dot = substr(strrchr($filename, "."), 1);return $dot;$extension = get_extension("06.11.07-reports.doc");[/code]I need to validate and use a str_replace function on all the fullstops except the final extension fullstop. How can I do this?Would something like this be along the right lines?[code=php:0]$filename = "06.11.07-reports.doc";$count_stops = substr_count($filename, ".");?? $filename = str_replace(first two dots with -, $filename); ??[/code] Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/ Share on other sites More sharing options...
joshi_v Posted November 7, 2006 Share Posted November 7, 2006 well it would be like this..[code]$file_name="06.11.07-reports.doc";$extension=substr($file_name,-4);#Here i am assuming that , file extension will always be 'doc'$file=substr($file_name,0,-4);$final_name=str_replace(".","-",$file).$extension;[/code]Regards,Joshi. Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120892 Share on other sites More sharing options...
Cep Posted November 7, 2006 Author Share Posted November 7, 2006 Thats true but the problems is I cannot assume in the code how long the extension is, if there is an extension and how many if any fullstops will appear in the filename proceeding the extension.I went for the count option because that way if 0 dots are found I can assume no file extension, if 1 is found then I need to obtain the extensions string and if more then 1 is found I need to replace the dots with hyphens except the extension dot :) Its that easy........ really hehehehehe Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120925 Share on other sites More sharing options...
Jenk Posted November 7, 2006 Share Posted November 7, 2006 [code]<?php$ext = substr($filename, strrpos($filename, '.'));?>[/code] Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120933 Share on other sites More sharing options...
Cep Posted November 7, 2006 Author Share Posted November 7, 2006 That only solves part of the problem but thanks. As mentioned the idea is that I will use str_replace on all the previous full stops so I wonder if this would be feasable,[code=php:0]$filename = "06.11.07-reports.doc";$count_stops = substr_count($filename, ".");switch ($count_stops) { case 0: // No extension echo "error"; break; case 1: // Correct number of dots $extarray = explode(".", $filename); $extension = $extarray[1]; break; default: // More than 1 dot replace all dots with hyphens $replacedname = str_replace(".", "-", $filename); // Find last hyphen $pos = strrpos($replacedname, "-"); // Replace last hyphen with a dot $propername = substr_replace($replacedname, ".", $pos); // Explode string on dot $extarray = explode(".", $propername); $extension = $extarray[1];}[/code] Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120940 Share on other sites More sharing options...
kenrbnsn Posted November 7, 2006 Share Posted November 7, 2006 You should look at the function [url=http://www.php.net/pathinfo]pathinfo()[/url].Example:[code]<?php$text_ext = array('doc','longextension');foreach ($text_ext as $ext) { $filename = '06.11.07-reports.' . $ext; $path_parts = pathinfo($filename); print_r($path_parts);}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120958 Share on other sites More sharing options...
Cep Posted November 7, 2006 Author Share Posted November 7, 2006 But isn't that assuming I know the name and extension mime type? Link to comment https://forums.phpfreaks.com/topic/26438-formating-a-file-name/#findComment-120967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.