Uploading Files with Curl

curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload

Courtesy of http://ariejan.net/2010/06/07/uploading-files-with-curl/

Render Partial if File Exists

If you ever want to render a partial but don’t want an error thrown you can either check for the existence of the file first

<%= render :partial => params[:controller]+"/sidebar" if File.exists?(RAILS_ROOT+"/app/views/"+params[:controller]+"/_sidebar.html.erb") %>

or you can catch the error that Rails throws

<%= render :partial => params[:controller]+"/sidebar" rescue nil %>

Listing Files and Directories with PHP

Listing all files and directories using PHP 5.

&lt;?php
$files = array(); $dir = dir(".");
while(false!==($file=$dir-&gt;read())):
  if(($file{0}!=".") && ($file{0}!="~") && (substr($file, -3)!="LCK")
    && ($file!=basename($_SERVER["PHP_SELF"]))):
      $files[$file] = stat($file); //
    endif;
endwhile;
$dir-&gt;close(); ksort($files); ?&gt;

&lt;table border=1 cellpadding=5&gt;
&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Size&lt;/th&gt;&lt;th&gt;Date&lt;/th&gt;&lt;/tr&gt;
&lt;?php foreach($files as $name =&gt; $stats): ?&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;?php print $name;?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php print $stats['size']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php print date("m-d-Y h:ia", $stats['mtime']); ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;?php endforeach; ?&gt;&lt;/table&gt;

Reading, Writing, Removing Files and Directories in Ruby

These aren’t all of them, but I think they are some of the most useful.

# making a directory in another directory that doesn't yet exist...
FileUtils.mkdir_p '/path/to/your/directory/that/doesnt/exist/yet'

# recursively remove a directory and the contents
FileUtils.rm_rf("/path/to/directory/you/want/to/delete")

# write a file from the contents of another file...
File.open("/path/to/the/file.ext", "wb") {|f| f.write(@your_other_file.read)}

the simple things with git

Git can track the file permissions on your source. This can be good, but for small projects on a webserver where permissions change from time to time, and permissions on development app don’t match or matter anyway, it’s often easier to just skip this check alltogether. For the application in question look in the .git directory in the application root. You’ll see a config file with the application specific directives. Open the file with your editor of choice and change the filemode to false. It’s set to true by default.

[core]
repositoryformatversion = 0
filemode = false