Home Backend Development PHP Tutorial File I/O processing on SAE platform_PHP tutorial

File I/O processing on SAE platform_PHP tutorial

Jul 20, 2016 am 11:12 AM
deal with Safety platform document friend user of Know consider limit

Friends who have used the SAE platform should know that due to platform security considerations, SAE limits users' use of local IO. But this may bring a lot of inconvenience to some traditional PHP projects, because they all have more or less local IO operations, such as Smarty's compilation template. To solve this problem, SAE provides the TmpFS function. TmpFS allows developers to temporarily read and write local IO through standard IO functions, which facilitates the porting of many non-SAE projects.

But TmpFS is not enough. Judging from the name, it is a temporary file system. Its life cycle is the same as that of a PHP request. That is, when the PHP request completes execution, all temporary files written to TmpFS will be destroyed. . TmpFS is a local temporary file, not a shared storage, and SAE is a fully distributed environment, so different requests cannot share operation files through TmpFS. For details, please refer to the SAE platform documentation.

Example: For example, I want to use TmpFS to make a counter (of course you can also use the counter service provided by SAE)

The code is as follows:

<strong><span  1</span> <?<span php
</span><span  2</span> <span $file</span>=SAE_TMP_PATH."/test.txt"<span ;
</span><span  3</span> <span if</span>(!<span file_exists</span>(<span $file</span><span )){
</span><span  4</span>     <span file_put_contents</span>(<span $file</span>,1<span );
</span><span  5</span>     <span echo</span> 1<span ;
</span><span  6</span> }<span else</span><span {
</span><span  7</span>     <span $n</span>=<span file_get_contents</span>(<span $file</span><span );
</span><span  8</span>     <span $n</span>++<span ;
</span><span  9</span>     <span echo</span> <span $n</span><span ;
</span><span 10</span>     <span file_put_contents</span>(<span $file</span>,<span $n</span><span );
</span><span 11</span> 
<span 12</span> <span }
</span><span 13</span> 
<span 14</span> 
<span 15</span> ?></strong>
Copy after login

I found that it is impossible to execute in else, because the temporary file system no longer exists with the end of each execution, so every time the code starts, it is judged (it is a new php request), that The temporary file no longer exists.

That is to say, zero-time files cannot be shared between two files or different time requests for a file.

In fact, if you read the SAE documents carefully, it is not difficult to find that

is included in the Wrappers provided by SAE.

KVDB -- saekv://

saekv:// is used to read and write KVDB. It is mainly used to save persistently stored data. The most commonly used scenario is to save configuration files

This can meet our requirements for creating and modifying persistent files

You must first before using this service.

The following is the test code for the counter:

<strong><span  1</span> <?<span php
</span><span  2</span> <span $file</span>="saekv://count.txt"<span ;
</span><span  3</span> <span if</span>(!<span file_exists</span>(<span $file</span><span )){
</span><span  4</span> <span file_put_contents</span>(<span $file</span>,1<span );
</span><span  5</span>     <span echo</span> 1<span ;
</span><span  6</span> }<span else</span><span {
</span><span  7</span>     
<span  8</span> <span $n</span>=<span file_get_contents</span>(<span $file</span><span );
</span><span  9</span>     <span echo</span> <span $n</span><span ;
</span><span 10</span>     <span $n</span>++<span ;
</span><span 11</span>     <span file_put_contents</span>(<span $file</span>,<span $n</span><span );
</span><span 12</span>     
<span 13</span>     
<span 14</span> }</strong>
Copy after login

That’s it.

There are two ways to delete files simultaneously

One way is to use PHP’s native deletion method unlink($file)

Another way is to delete the kvdb database provided by SAE:

Code:

<strong><span 1</span> <span $file</span>="saekv://count.txt"<span ;
</span><span 2</span> <span $kv</span>=<span new</span><span  SaeKV();
</span><span 3</span> <span $kv</span>-><span init();
</span><span 4</span> <span if</span>(<span $kv</span>->delete("count.txt"<span )){
</span><span 5</span> <span echo</span> "ok"<span ;
</span><span 6</span> }<span else</span><span {
</span><span 7</span> <span echo</span> "no"<span ;
</span><span 8</span> }</strong>
Copy after login

For details, please refer to the KVDB service document. count.txt is the key value...

Reference documentation: SAE platform documentation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440414.htmlTechArticleFriends who have used the SAE platform should know that due to platform security considerations, SAE limits users’ access to local IO of use. But this may bring a lot of inconveniences to some traditional PHP projects...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
What are the limitations and considerations for C++ function overloading? What are the limitations and considerations for C++ function overloading? Apr 13, 2024 pm 01:09 PM

Restrictions on function overloading include: parameter types and orders must be different (when the number of parameters is the same), and default parameters cannot be used to distinguish overloading. In addition, template functions and non-template functions cannot be overloaded, and template functions with different template specifications can be overloaded. It's worth noting that excessive use of function overloading can affect readability and debugging, the compiler searches from the most specific to the least specific function to resolve conflicts.

Implementing Machine Learning Algorithms in C++: Security Considerations and Best Practices Implementing Machine Learning Algorithms in C++: Security Considerations and Best Practices Jun 01, 2024 am 09:26 AM

When implementing machine learning algorithms in C++, security considerations are critical, including data privacy, model tampering, and input validation. Best practices include adopting secure libraries, minimizing permissions, using sandboxes, and continuous monitoring. The practical case demonstrates the use of the Botan library to encrypt and decrypt the CNN model to ensure safe training and prediction.

Security configuration and hardening of Struts 2 framework Security configuration and hardening of Struts 2 framework May 31, 2024 pm 10:53 PM

To protect your Struts2 application, you can use the following security configurations: Disable unused features Enable content type checking Validate input Enable security tokens Prevent CSRF attacks Use RBAC to restrict role-based access

PHP Microframework: Security Discussion of Slim and Phalcon PHP Microframework: Security Discussion of Slim and Phalcon Jun 04, 2024 am 09:28 AM

In the security comparison between Slim and Phalcon in PHP micro-frameworks, Phalcon has built-in security features such as CSRF and XSS protection, form validation, etc., while Slim lacks out-of-the-box security features and requires manual implementation of security measures. For security-critical applications, Phalcon offers more comprehensive protection and is the better choice.

How to enhance the security of Spring Boot framework How to enhance the security of Spring Boot framework Jun 01, 2024 am 09:29 AM

How to Enhance the Security of SpringBoot Framework It is crucial to enhance the security of SpringBoot applications to protect user data and prevent attacks. The following are several key steps to enhance SpringBoot security: 1. Enable HTTPS Use HTTPS to establish a secure connection between the server and the client to prevent information from being eavesdropped or tampered with. In SpringBoot, HTTPS can be enabled by configuring the following in application.properties: server.ssl.key-store=path/to/keystore.jksserver.ssl.k

Applications and limitations of inline template functions Applications and limitations of inline template functions Apr 28, 2024 pm 02:33 PM

Inline template functions insert code directly into the call point without generating a separate function object. Applications include code optimization, performance improvement, constant evaluation, and code simplification. But be aware of its limitations, such as longer compilation times, increased code size, reduced debuggability, and limitations across compilation units.

How should the Java framework security architecture design be balanced with business needs? How should the Java framework security architecture design be balanced with business needs? Jun 04, 2024 pm 02:53 PM

Java framework design enables security by balancing security needs with business needs: identifying key business needs and prioritizing relevant security requirements. Develop flexible security strategies, respond to threats in layers, and make regular adjustments. Consider architectural flexibility, support business evolution, and abstract security functions. Prioritize efficiency and availability, optimize security measures, and improve visibility.

Which wallet is safer for SHIB coins? (Must read for newbies) Which wallet is safer for SHIB coins? (Must read for newbies) Jun 05, 2024 pm 01:30 PM

SHIB coin is no longer unfamiliar to investors. It is a conceptual token of the same type as Dogecoin. With the development of the market, SHIB’s current market value has ranked 12th. It can be seen that the SHIB market is hot and attracts countless investments. investors participate in investment. In the past, there have been frequent transactions and wallet security incidents in the market. Many investors have been worried about the storage problem of SHIB. They wonder which wallet is safer for SHIB coins at the moment? According to market data analysis, the relatively safe wallets are mainly OKXWeb3Wallet, imToken, and MetaMask wallets, which will be relatively safe. Next, the editor will talk about them in detail. Which wallet is safer for SHIB coins? At present, SHIB coins are placed on OKXWe

See all articles