Snippets v2

Languages
My Snippets

Generate a Safe Filename from string Login or Register to Bookmark this snippet

Description PHP

This function strips all bad characters from a filename, that has perhaps been uploaded from a windows environment but has characters not allowed in a unix filesystem

The Code Download
  1. <?php
  2. function GenerateSafeFileName($filename) {
  3.     $filename = strtolower($filename);
  4.     $filename = str_replace("#","_",$filename);
  5.     $filename = str_replace(" ","_",$filename);
  6.     $filename = str_replace("'","",$filename);
  7.     $filename = str_replace('"',"",$filename);
  8.     $filename = str_replace("__","_",$filename);
  9.     $filename = str_replace("&","and",$filename);
  10.     $filename = str_replace("/","_",$filename);
  11.     $filename = str_replace("\","_",$filename);
  12.    $filename = str_replace("?","",$filename);
  13.    return $filename;
  14. }
  15. ?>
Credits Contact JC

Added by JC on 11th November, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment