Jump to content

Formating a file name


Cep

Recommended Posts

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

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

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

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

Archived

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

×
×
  • 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.