Other 下载

<?php
	if(isset($_GET["file"]) && $_GET["file"] != "") {
		if(file_exists($_GET["file"])) {
			header("Content-type: application/x-download");
			header("Content-Length: ".filesize($file)); 
			header('Content-Disposition: attachment; filename="'.basename($_GET["file"]).'"');
			readfile($_GET["file"]); 
			exit();
		} else {
			exit("File not found");
		}
		
	} else { exit(); }
?>

Bash 轻松的端口转发

#!/bin/sh

LOCALPORT=$1
REMOTEHOST=$2
REMOTEPORT=$3

socat -d -d -lmlocal2 \
    TCP4-LISTEN:${LOCALPORT},fork,range=127.0.0.1/0,reuseaddr \
    TCP4:${REMOTEHOST}:${REMOTEPORT}

XML 用于将BLOB从文件插入数据库的ETL脚本

<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
    <etl>
        <connection driver="hsqldb" url="jdbc:hsqldb:file:tracks" user="sa" classpath="hsqldb.jar"/>
        <script>
            CREATE TABLE Track (
              ID INT,
              ALBUM_ID INT,
              NAME VARCHAR(100),
              DATA LONGVARBINARY
            );
            <!-- Inserts file with path relative to ETL script location -->
            INSERT INTO Track(id, album_id, name, data) VALUES
                   (1, 1, 'Song1.mp3', ?{file 'song1.mp3'});
            <!-- Inserts file from an external URL-->
            INSERT INTO Track(id, album_id, name, data) VALUES
                   (2, 2, 'Song2.mp3', ?{file 'http://musicstoresample.com/song2.mp3'});
        </script>
    </etl>

XML 将表从一个数据库复制到另一个数

<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd">
<etl>
  <connection id="in" driver="hsqldb" url="jdbc:hsqldb:file:demo" 
              classpath="hsqldb.jar" user="sa"/>
  <connection id="out" driver="oracle" url="jdbc:oracle:thin:@localhost:1521:ORCL" 
              classpath="ojdbc14.jar" user="scott" password="tiger"/>
  <!-- Copy all table rows from one to another database -->
  <query connection-id="in">
      SELECT * FROM Src_Table --Selects all rows
      <!-- For each row executes insert -->  
      <script connection-id="out"> 
          INSERT INTO Dest_Table(ID, Name) 
          VALUES (?id,?{first_name+' '+last_name})
      </script>
  </query>
</etl>

JavaScript 在第一焦点清晰的形式领域

<html>
<head>
<title>Clear form field on initial focus</title>
<script type="text/javascript">
function clearOnInitialFocus ( fieldName ) {
  var clearedOnce = false;
   document.getElementById( fieldName ).onfocus = (function () {
    if (clearedOnce == false) {
      this.value = '';
      clearedOnce = true;
    }
  })
}
window.onload = function() { clearOnInitialFocus('myfield');
</script>
</head>
<body>
<h1>Clear form field on initial focus</h1>
<input name="myfield" id="myfield" value="default message">
</body>
</html>

JavaScript 加载浏览器主页

window.home();

Ruby 重定向页面上显示捕获错误

def add_to_cart
  begin
    product = Product.find(params[:id])
  # if can not find product id, catch exception, raise error to be
  # displayed on 'index' page
  rescue ActiveRecord::RecordNotFound
    logger.error("Atempt to access invalid product #{params[:id]}")
    flash[:notice] = "invalid product"
    redirect_to :action => :index
  else
  # if can find product id, continue to add product to cart
    @cart = find_cart
    @cart.add_product(product)
  end
end

# RHTML code
<% if flash[:notice] -%>
	<div id="notice"><%= flash[:notice] %></div>
<% end -%>

Ruby 添加按钮到视图

<%= button_to "Empty cart", :action => :empty_cart %>

Ruby 从一种方法重定向到另一种方法

redirect_to :action => :index

Ruby 适用于TextDrive的Capistrano食谱

## Set these vars up

set :application, "your_appname"                     # The name of the app
set :user, "your_username"                           # Your Textdrive username
set :domain, 'your_servername.textdrive.com'         # Your domain
set :path, "/users/home/#{user}"                     # The path to root of your site
set :port, YOUR_PORT                                 # Your port from Textdrive
set :apps_directory, "railsapps"                     # The directory where you store your rails apps


## Don't have to worry about anything below here

set :repository, "http://url_for.yoursvnrepos.com/svn/#{application}/trunk"
set :svn_username, Proc.new { "your_svn_username --password your_svn_password" }
    
set :deploy_to, "#{path}/#{apps_directory}/#{application}" 

role :web, domain
role :app, domain
role :db,  domain, :primary => true

# Space saving by Visi http://blogs.law.harvard.edu/vgondi/about
set :keep_releases, 3
set :checkout, "export"

task :cold_deploy do
  transaction do
    update_code
    symlink
  end
  
  run "chmod 755 #{deploy_to}/current/script/spawn" 
  run "#{deploy_to}/current/script/spawn"
  restart  
end

task :restart do
  run "#{deploy_to}/current/script/process/reaper -a reload"
end

task :after_deploy do
  #run "cp #{path}/etc/#{application}.yml #{path}/sites/#{application}/current/config/database.yml"

  #setup links to directories in the shared directory
  delete "#{current_path}/public/photos", :recursive => true
  run "ln -nfs #{shared_path}/photos #{current_path}/public/photos" 
end

desc <<-DESC
Removes unused releases from the releases directory. By default, the last 5
releases are retained, but this can be configured with the 'keep_releases'
variable. This will use sudo to do the delete by default, but you can specify
that run should be used by setting the :use_sudo variable to false.
DESC
task :cleanup do
  count = (self[:keep_releases] || 5).to_i
  if count >= releases.length
    logger.important "no old releases to clean up"
  else
    logger.info "keeping #{count} of #{releases.length} deployed releases"
    directories = (releases - releases.last(count)).map { |release|
      File.join(releases_path, release) }.join(" ")

    delete "#{directories}", :recursive => true
  end
end

task :after_setup, :roles=>[:web, :app, :db] do
  
end