Upload PHP to the SAE problem to use IO

Source: Internet
Author: User
Tags upload php wrappers in domain

Application Porting Guide

One, why transplant applications



The SAE prohibits IO write operations, and the code directory cannot be written. This means that the normal program upload pictures, generate cache and other operations can not run properly on the SAE, this time you need to modify the code before your program to run on the SAE.



Why does SAE prohibit IO write operations?



With a distributed architecture design, the application code is deployed on multiple front-end servers, and each access request may reach different servers. (e.g.):


Suppose you now have a, B, C, D four servers. The user uploads a picture to a server, the second access request may reach the B server, and the picture saved on the a server will not be available at this time.



Using storage services such as Memcachex and storage instead of traditional IO operations, SAE is more efficient than traditional IO read and write operations and effectively solves the problem of poor program performance due to IO bottlenecks.



In addition, many sites are attacked because the server has write permissions, program code can be modified by hackers, SAE prohibit write operations, but also improve the security of the server.

So SAE prohibits local IO write operations in order to improve performance and security. Developers can use services such as STORAGE,MEMCACHE,KVDB to store data that needs to be written.


Second, quickly transplant your program


Using wrappers

Although the SAE prohibits IO write operations, it does not prohibit write operations such as Fwrite,file_put_contents, and the SAE also provides wrappers services, which makes the porting process easier. Suppose we want to port the following code to the SAE.

File_put_contents (' test.php ', ');
Include ' test.php ';
?>
You just need to prefix the file address.

File_put_contents (' saemc://test.php ', ');
Include ' saemc://test.php ';
?>

If the prefix of the address is saemc://for read and write operations in Memcache, a prefix of saestor://indicates read and write operations in storage. A prefix of saekv://is the operation on Kvdb. This is the function of wrappers.

Attention:

1, before using Wappers, the corresponding service needs to be initialized first.

Initialize Memcache: Go to memcache service Management backend http://sae.sina.com.cn/?m=mcmng, then select the specified app in the "My App" in the top right corner of the webpage. Click the button "click here to initialize MC" to initialize the memcache.

Initialize storage: Enter the storage service Management background http://sae.sina.com.cn/?m=storage, select "Click Create a Domain" button to create the app. This allows the creation of multiple domain, where developers use wrappers to upload files to which domain, depending on the first directory name of the file address. Addresses such as "Saestor://upload/image/1.jpg" will save the file in domain named upload.

In addition, MySQL and Kvdb also need to be initialized before use, if the program is useful to these services, please initialize first.

2, frequently-operated caches are not suitable for storage in storage.

Storage read-write efficiency is much lower than Kvdb and memcache. Frequently-operated caches are not suitable for storage in storage, so try not to use saestor://in addition to manipulating the files uploaded by users.

In particular, some program code fragments of the cache. As in the above example, if you use code such as include ' saestor://xxx ', this will make the program less performance.

Once you understand the usage of wrappers, you can start porting the program.

Quick location with Error

The developer can quickly navigate to the code that the program needs to modify by using the SAE error prompt.

First, make sure that your app turns on the wrong tip. Go to the app's "code management" background http://sae.sina.com.cn/?m=vermng to see if the corresponding version of "error Prompt" is turned on (default is on). and check the program. There is no code similar to error_reporting (0) that masks the error message. If so, please comment them out first.

Then upload the program to the SAE. If there is an incompatibility, the error message will tell you the specific line of the specific file.

If a prompt like this appears:

Sae_warning:file_put_contents (./xxx.php) [function.file-put-contents]: failed to open stream:permission denied in Xxx.php on line 2

The description program is in IO write operation, only need to add wrappers prefix to address, can achieve compatibility.

If your program is well packaged, changing the code for IO write operations is often straightforward. For example, thinkphp defines Runtime_path constants. All IO write files will be stored in the Runtime_path constant specified folder, porting thinkphp program, only need to change the Runtime_path constant, plus saemc://prefix. In fact, other frameworks or packaging is a good program, also, just change one line of code, can be compatible with IO write operations. If you are re-using the thinkphp development program, it is recommended to use the SAE version of thinkphp.

Note: Some operations are not valid after using wrappers. such as file_exists, you can directly comment out such functions.


If a prompt similar to the following appears:

Sae_warning:mysql_connect () [Function.mysql-connect]: This app isn't authorised in xxx.php on line 2

The program is using the MySQL database, and it needs to initialize MySQL and import the database. You can click on the "Manage MySQL" button after initialization and use phpMyAdmin for data import. When your data is large, you can use the Deferredjob service to import it.

After importing the database, change the database user name, password, etc. in the original program to SAE. The database user name, password, and database name of the SAE are represented using constants. Can be seen in the MySQL service management office. The SAE MySQL database supports distributed, capable of connecting two databases. If your original program only supports single-database operations, connect to the main library domain name.

When your program does not have an error on the SAE, your program will be able to run on the SAE initially. You should also check to see if some of the details are implemented. such as uploading image function. It is recommended to store uploaded images or other attachments in storage. After the picture is stored in storage, the image's access address is not the same as before. You need to use storage's Geturl method to get a picture access address, such as:

$s =new saestorage ();
$url = $s->geturl (' domain ', ' filepath ');//Get the picture address, filepath the path to the picture in storage
?>

Next, developers can further optimize the program to achieve better performance.

Third, performance optimization

We have provided some suggestions for optimization.

1 IO operations generate files for differentiated storage

(1) It is recommended to put the template compilation cache or other program code fragment cache in Memcache, firmly oppose the template compilation cache in storage, because storage read and write efficiency is lower than memcache, depositing them storage will cause your program to run slower.

(2) Store uploaded images or attachments in storage. If your picture needs anti-theft chain and other functions, you can also in the SAE service management background, by setting the domain properties can easily implement the anti-theft chain.

(3) store some fixed caches in kvdb. There are some caches that are not suitable for storage in memcache because the cache in memcache may disappear, such as the cache that was first deposited in memcache when the memcache space is low. So you deposit the cache to the Memcache, and when you read it you need to determine if the cache exists, and if there is no regeneration. However, some caches do not change after they are generated once, and it is not necessary to determine whether or not it exists when reading the cache. This type of cache is recommended for storage using KVDB.

2 realization of database read/write separation

The main library of the SAE MySQL database is read-write and readable from the library. The query uses less cloud beans from the library and responds faster, so we recommend that you try to achieve a master-slave separation. Many frameworks now support read and write separations, and simply configure them. If the program only supports a single database, you can try to determine when executing SQL if the select is connected from the library.

More than 3 use of services provided by SAE

SAE offers a number of efficient services and recommends that the program use the SAE service wherever possible. For example, the program has a leaderboard function, you can use the Rank service implementation, the function of the verification code, you can use the Saevcode service, the ability to send mail, you can use the mail service and so on. Please check out the documentation for each service to learn about them.

4 Consider using native interfaces

Wrappers is convenient and simple, but its efficiency is certainly not as good as the native memcache or storage interface. If you care about efficiency issues, consider porting using the original interface. The method of porting using native interfaces can be consulted: http://qing.weibo.com/1631767865/6142cd3933000cj9.html

Four, FAQs

1 Implementing Pseudo-static

The SAE does not support. htaccess files, but pseudo-static can be implemented through the AppConfig service.

2 Generating a static page

Some programs have a mechanism for generating pure static HTML. You can use KVDB to store HTML static page data, because the SAE prohibit IO write operation, cannot achieve true pure static, we can use pseudo-static way to reach the same effect, the following is a simple example.
Suppose the program generates an HTML file with the following code before it is ported:

File_put_contents (' html/index.html ', ' htmlcontent ');
?>

This allows the user to enter the address via the browser http://Your domain name/html/index.html access to a purely static page.
Now we're going to port the program to the SAE.
First, save the HTML data to Kvdb.

File_put_contents (' saekv://html/index.html ', ' htmlcontent ');
?>

Write a pseudo-static statement using Config.yaml

-Rewrite:if (Path ~ "/html/(. *)") goto "/readhtml.php?path=html/$1"

If a user accesses an HTML directory, it is pseudo-static to the readhtml.php file to read the static data.
The code for readhtml.php is:

echo file_get_contents (' saekv://'. $_get[' path ');
?>

In this way, the user can also enter the address via the browser, http://Your domain/html/index.html access to the data on the SAE.

3 log records.
Some programs have logging function, if log read and write too frequently, not suitable for log files into storage, we recommend using Sae_debug to implement logging. However, the log content is also output to the browser when the Sae_debug logs are logged. Many programs want to be able to log the actual surface, we can be implemented through the encapsulation function.
Such as:
function Sae_log ($msg) {
Sae_set_display_errors (false);//Shutdown Information output
Sae_debug ($msg);//Logging
Sae_set_display_errors (TRUE);//log and then open the information output, otherwise it will prevent the normal error message display
}
?>

Logs logged through the Sae_debug function can be viewed in the log center in the app management background, which is a debug type log, and you need to select the debug type to view in the Search box drop-down menu.

4 Cache Sharing Issues

SAE each application can create multiple versions, but these versions share the same MEMCACHE,KVDB and other services, and sometimes are prone to cache sharing issues. For example, an application created two versions, put in the same program, the program has a template cache mechanism, may appear, only modified version 1 templates, but found that version 2 of the content has been modified. We want to avoid the same cache names between different versions. Developers can use the $_server[' http_appversion '] variable to add the app's version number to the cache name

5 Memcache cache requires an update mechanism

Memcache cache because of the possibility of disappearing, so when reading the memcache cache should be to determine whether the cache exists, if there is no regeneration cache, otherwise, if the memcache cache is deleted and the program does not update the cache mechanism, the site may not be able to access normal. Developers can manually empty the Memcache cache in the application's Memcahe service management background after the program has been ported, and then test the site to see if it works properly. It is recommended that you use KVDB storage for caches that do not require an update mechanism.

6 How to get the storage domain name

Sometimes the picture access path of the program can define the prefix, we just need to replace the prefix with the storage domain name can be. Ways to get storage domain names:

$s =new saestorage ();
$domain =rtrim ($s->geturl (' domain ', '), '/');//Note that the second parameter of GETURL is empty
?>

7 You can't use wrappers in some places.
Not all file addresses are compatible with wrappers corresponding prefixes. such as the following code:

$img =imagecreatefrompng ("Http://sae.sina.com.cn/static/image/logo.beta.new.png");
... After some column of the picture processing function processing ...
Imagepng ($img, ' saestor://upload/logo.png ');//want to save the processed picture through wrappers
?>

The above code is not able to save the picture in storage.

Developers can use temporary files to solve the problem above.

$img =imagecreatefrompng ("Http://sae.sina.com.cn/static/image/logo.beta.new.png");
... After some column of the picture processing function processing ...
Imagepng ($img, Sae_tmp_path. Logo.png ');//Save As Temp file
File_put_contents (' Saestor://upload/logo.png ', file_get_contents (Sae_tmp_path. Logo.png '));
?>

It is important to note that although the file address in the program is prefixed with wrappers, check that the file address is being used on a function that does not support wrappers.

General file manipulation functions are supported for wrappers, such as File_put_contents,fwrite,file_get_contents,include,file_exists, filemtime,move_uploaded _file and so on. Only individual functions with special functions do not support wrappers.

8 SAE disabled some functions and classes

For platform security reasons, the SAE has disabled some of the functions of the class, view details
http://sae.sina.com.cn/?m=devcenter&catId=220

Use other functions with the same functionality instead of disabled functions, such as using simplexml instead of DOMDocument for XML parsing.

Five, submit to the application warehouse

Developers can submit the migrated program to the SAE application repository so that others can quickly get to your program by installing one click from the application repository.

We recommend that you write a "transplant record" when you transplant the program. Submit your migration records to us as you submit your application. This allows us to review your application.

The transplant records need to be written clearly where the transplant program was modified, what SAE services were used, etc.

For more details on submitting to the application warehouse, see http://sae.sina.com.cn/?m=devcenter&catId=230
or direct access to the following address: Http://sae.sina.com.cn/?m=apps&a=step_sae_app

Upload PHP to the SAE problem to use IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.