<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: filename code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 11:58:12 GMT</pubDate>
    <description>DZone Snippets: filename code</description>
    <item>
      <title>Generate unique filenames in PHP</title>
      <link>http://snippets.dzone.com/posts/show/5579</link>
      <description>This code is from &lt;a href="http://www.weberdev.com/get_example-3543.html"&gt;http://www.weberdev.com/get_example-3543.html&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;It produces filenames similar to this: 4293d8fd-ab63-7c82.tmp&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function uniqueFilename($strExt = '.tmp') {&lt;br /&gt;	// explode the IP of the remote client into four parts&lt;br /&gt;	$arrIp = explode('.', $_SERVER['REMOTE_ADDR']);&lt;br /&gt;	// get both seconds and microseconds parts of the time&lt;br /&gt;	list($usec, $sec) = explode(' ', microtime());&lt;br /&gt;	// fudge the time we just got to create two 16 bit words&lt;br /&gt;	$usec = (integer) ($usec * 65536);&lt;br /&gt;	$sec = ((integer) $sec) &amp; 0xFFFF;&lt;br /&gt;	// fun bit--convert the remote client's IP into a 32 bit&lt;br /&gt;	// hex number then tag on the time.&lt;br /&gt;	// Result of this operation looks like this xxxxxxxx-xxxx-xxxx&lt;br /&gt;	$strUid = sprintf("%08x-%04x-%04x", ($arrIp[0] &lt;&lt; 24) | ($arrIp[1] &lt;&lt; 16) | ($arrIp[2] &lt;&lt; 8) | $arrIp[3], $sec, $usec);&lt;br /&gt;	// tack on the extension and return the filename&lt;br /&gt;	return $strUid . $strExt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 03 Jun 2008 20:42:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5579</guid>
      <author>jeffreybarke (Jeffrey Barke)</author>
    </item>
    <item>
      <title>Rename *nix filenames</title>
      <link>http://snippets.dzone.com/posts/show/2793</link>
      <description>&lt;br /&gt;I am a bit slow sometimes and it took me a while to find this unix command that most are probably already aware of. rename is a very useful tool. The example I have here will susbtitute "foo" for "bar" of any file with foo in the name. The really nice thing about this is that it can use and regular expression syntax (ie. "^", "$", etc..).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rename -n 's/foo/bar/' *foo*&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 07 Oct 2006 02:31:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2793</guid>
      <author>muipotu (Mui Potu)</author>
    </item>
    <item>
      <title>Return the extension from a file name</title>
      <link>http://snippets.dzone.com/posts/show/2555</link>
      <description>// Return the extension from a file name&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/(.*\.)(.*$)/.match(File.basename(file_name))[2]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Sep 2006 02:06:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2555</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>PHP filename bad character filter</title>
      <link>http://snippets.dzone.com/posts/show/1506</link>
      <description>// Function to filter out bad characters in a given filename&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function replace_bad_filename_chars($filename) {&lt;br /&gt;  $filtered_filename = "";&lt;br /&gt;&lt;br /&gt;  $patterns = array(&lt;br /&gt;    "/\s/", # Whitespace&lt;br /&gt;    "/\&amp;/", # Ampersand&lt;br /&gt;    "/\+/"  # Plus&lt;br /&gt;  );&lt;br /&gt;  $replacements = array(&lt;br /&gt;    "_",   # Whitespace&lt;br /&gt;    "and", # Ampersand&lt;br /&gt;    "plus" # Plus&lt;br /&gt;  );&lt;br /&gt;  &lt;br /&gt;  $filename = preg_replace($patterns,$replacements,$filename);&lt;br /&gt;  for ($i=0;$i&lt;strlen($filename);$i++) {&lt;br /&gt;    $current_char = substr($filename,$i,1);&lt;br /&gt;    if (ctype_alnum($current_char) == TRUE || $current_char == "_" || $current_char == ".") {&lt;br /&gt;      $filtered_filename .= $current_char;&lt;br /&gt;    }&lt;br /&gt;  }     &lt;br /&gt;        &lt;br /&gt;  return $filtered_filename;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Feb 2006 09:43:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1506</guid>
      <author>mvidberg ()</author>
    </item>
  </channel>
</rss>
