Shorten filename if necessary

  define("FILENAME_MAX_LENGTH",     10);

  $file1_name = "myfilenameisthis.txt";

  //Shorten the file name if its too long
  $FilenameExtension = pathinfo($file1_name, PATHINFO_EXTENSION);
  $FilenameBasename = pathinfo($file1_name, PATHINFO_FILENAME);
  $FilenameExtension = $FilenameExtension ? '.' . $FilenameExtension : '';   //If extension is present, add the dot
  $MaxBasenameLength = FILENAME_MAX_LENGTH - strlen($FilenameExtension);

  if ($MaxBasenameLength < 1)
  {
    //Not enough space for even 1 character of the basename
    $file1_name = substr($FilenameBasename, 0, 1) . $FilenameExtension;   //<<<<<THIS WILL BE TOO LONG!!!!
  }
  else if (strlen($FilenameBasename) > $MaxBasenameLength)
  {
    //Filename needs shortening
    $file1_name = substr($FilenameBasename, 0, $MaxBasenameLength) . $FilenameExtension;
  }
  else
  {
    //No need to shorten
    //$file1_name = $file1_name;
  }
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *