Reference reading
- SAE Storage API Documentation
- Storage Description Document
- Storage Large File Upload instructions
What is storage?
Because the SAE disabled the code environment of the local read and write, but in the process of the Web site, there will be a file read and write, attachment preservation problem, at this time the access storage appearances, storage instead of the regular upload directory ~
How to use storage in SAE?
See the following illustration:
Enter the name of domain, other suggestions do not need to fill in, especially the anti-theft chain that block, for unfamiliar people, settings may cause storage within the resources can not access, and so thoroughly familiar with the storage work principle and then set no later: Here I create a domain named lazy for teaching.
So you've created storage,
You can click "Manage" to manage the files in storage, upload, batch upload, delete and so on in the management panel of SAE.
This article mainly describes the use of storage from a code perspective, and a concrete example of how to write a user-uploaded picture to storage.
First real Storage
We can create the following script to see the storage now
<?PHP$stor=Newsaestorage ();$domain= ' lazy ';//the name of the domain I just created$filename= ' Remote_file.txt ';$content= ' Hello lazy ';$stor->write ($domain,$filename,$content );Echo($stor->read ($domain,$filename));//get the contents of a fileEcho' <br> ';Echo($stor->geturl ($domain,$filename));//get the absolute address of the file?>
Run can get the following results (http://lazydemo.sinaapp.com/storage/know_storage.php)
Hello lazyhttp://lazydemo-lazy.stor.sinaapp.com/remote_file.txt
After the above examples believe that most people should figure out what storage is, then give an example below to explain if the file is uploaded directly from the form form to storage.
Index.html
<HTML><title>Sae Storage Demo</title><Div><formID= "Pic_upload"Action= "recieve.php"Method= "POST"enctype= "Multipart/form-data"Target= "_self"name= "Pic_upload"Action-type= "form"Node-type= "form"><inputtype= "File"name= "File"class= "File"ID= "Imgfile"value= "Demo" /><inputtype= "Submit"value= "Upload"class= "Write_weibo"name= "Submit" /></form></Div>
recieve.php
<?PHP$stor=Newsaestorage ();$domain= ' lazy ';//the name of the domain I just created$url=NULL;if($_files["File"] ["Tmp_name"]! =NULL){$fileDataName=$_files["File"] ["Name"];//add images to upload to storage$dumpdata=file_get_contents($_files["File"] ["Tmp_name"]);$DOWLOADURL=$stor->write ($domain,$fileDataName,$dumpdata);//just write it, okay?$url=$stor->geturl ($domain,$fileDataName);//If you upload a picture's processing addressEcho"Uploaded file:";Echo($url);}?>
Test Address: http://lazydemo.sinaapp.com/storage/
About working with wrapper (saestor://) storage
SAE provides wrapper to operate the storage, which facilitates the use of file_get_contents and other functions, further reducing the cost of learning, below I use a simple example Apple told how to use saestor://to operate storage. See the following example:
<? PHP file_put_contents (' Saestor://lazy/testwrapper.txt ', ' Hello Wrapper '); // Write a Hello wrapper to the Testwrapper.txt file $content file_get_contents (' Saestor://lazy/testwrapper.txt '); Var_dump ($content);? >
Visit: http://lazydemo.sinaapp.com/storage/storage_wrapper.php can see the result is:
String ("Hello wrapper")
This tutorial all source code Packaging:
Http://lazydemo.sinaapp.com/storage/storage.zip
Sina App Engine (SAE) Introductory Tutorial (7)-Storage use