Using Form-based File Upload in Snoopy

Snoopy is capable of handling form-based file uploads according to RFC's 1867

and 2388.  This note describes how to use Snoopy in a number of different file

upload scenarios.  Snoopy will handle multiple file upload fields per request

and multiple files per field.  Snoopy can also suggest a different filename in

the upload request from the "real" local filename.  Snoopy can also put

Content-type into the file upload request on a per file basis.

 

 

EXAMPLE 1: SIMPLE FILE UPLOAD

 

In this example there is a single field with a single file to be uploaded and

nothing fancy.  Note that it is necessary to call the set_submit_multipart()

function for the file upload to work.  The filename suggested to the server

will be "myfile.txt" since the path will be removed.

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.cgi";

$upload_vars = array();

$upload_files["FIELD1"] = "/home/me/myfile.txt";

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";

 

 

 

EXAMPLE 2: MULTIPLE FILES IN A SINGLE FIELD

 

Snoopy will support sending multiple files in response to a single form field.

This example works when sending multiple files to an Apache/PHP server.  Other

servers may not support this in quite the same way.

 

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.php";

$upload_vars = array();

$upload_files["FIELD1[]"] = array("/home/me/myfile1.txt", "/home/me/myfile2.txt");

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";

 

 

 

EXAMPLE 3: SPECIFYING FILENAMES and CONTENT TYPES

 

Some hosts are fussy about the filename that is passed to them by the file

upload request.  We can overcome this by getting Snoopy to suggest a different

filename in the request than the "real" local filename.  We can also get

Snoopy to specify an optional content type for each file.  This is achieved by

having each file entry be an array of parameters instead of a simple filename.

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.cgi";

$upload_vars = array();

$upload_files["FIELD1[]"] = array(

array("name" => "/home/me/myfile1.txt",

     "remotename" => "C:\UPLOAD.TXT",

     "type" => "text/plain"),

 

array("name" => "/home/me/myfile2.tiff",

     "remotename" => "C:\UPLOAD.TIF",

     "type" => "image/tiff")

);

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";

PHP设计模式-单例模式

    经过良好设计的系统一般通过方法调用来传递关系实例,每个类都会与背景环境保持独立,并通过清晰的通信方式来与系统中其他部分进行写作。

     假如我们需要一个类来保存系统信息,我们要保证系统中的所有对象都使用同一个 1.系统信息对象应该被系统中的任何对象使用

2.系统对象不应该会被存储在会被复写的全局变量中

3.系统中不应该超过一个系统信息对象,也就是说Y对象可以设置系统对象的一个属性,而Z对象不需要通过其他对象就能直接获得该属性的值

 

下面创建一个无法从外部实例化的类:

class Preferences{
    private $props = array();
    private function __construct(){}

    public function setProperty($key, $val){
        $this->props[$key] = $val;
    }

    public function getProperty($key){
        return $this->props[$key];
    }
}

    当然,目前的Preferences是完全不能用的,我们设置了一个不合常理的限制,构造函数被生命为private, 客户端无法实例化对象。

    不过我们可以使用静态方法和静态属性来实例化对象。

class Preferences{
    private $props = array();
    private static $instance;

    private function __construct(){}

    public static function getInstance(){
        if(empty(self::$instance)){
            self::instance = new Perference();
        }
        return self::$instance;
    }

    public function setProperty($key, $val){
        $this->props[$key] = $val;
    }

    public function getProperty($key){
        return $this->props[$key];
    }
}

instance 内部被设置为private以及static,因此不能被从类外部进行访问。而getinstance是public static的可以在脚本的任何地方进行调用。

$pref = Preferences::getInstance();
$pref -> SetProperty('name','matt');

unset($pref);//移除引用

$pref2 = Preferences::getInstance();
print $pref2->getProperty("name")."\n"; //该属性值没有丢失

输出matt

 

    静态方法不能访问普通的对象属性,应为根据静态定义,它只能被类而不是对象调用。但是静态方法可以访问一个静态属性所以当getInstance被调用时,我们会检查Preferences::$instance属性,如果为空,那么创建一个Preferences对象实例并把它保存在$instance属性中,然后我们把实例返回给调用代码。因为getInstance()是Perferences类的一部分,所以尽管构造函数是私有的,但是实例化Perferences对象完全没有问题。

 

    单例模式适用于代替全局变量使用,单例在itong任何地方都可以被访问,所以它可能会导致很难调试的依赖关系。如果改变一个单例,那么所有使用该单例的类都会受到影响。

 

    1。单例模式防止其它对象对自己的实例化,确保所有的对象都访问一个实例。

    2。
因为由类自己来控制实例化进程,类就在改变实例化进程上有相应的伸缩性。

—————————————————————————————————————————————

在JAVA里的单例



因为你每做一次数据库的操作,都必须创建一个session, 这时候用单例模式是最好的,每次都只是同一个实例,就不会象上面那样出错啦 

这个是为了节省资源吧。。

 

  最常见的要数DB类了。几乎所有的PHP框架都是这个套路