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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Full analysis of Go language file renaming operation Full analysis of Go language file renaming operation Apr 08, 2024 pm 03:30 PM

The os.Rename function is used in Go language to rename files. The syntax is: funcRename(oldpath,newpathstring)error. This function renames the file specified by oldpath to the file specified by newpath. Examples include simple renaming, moving files to different directories, and ignoring error handling. The Rename function performs an atomic operation and may only update directory entries when the two files are in the same directory. Renames may fail across volumes or while a file is in use.

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.

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.

How to implement PHP security best practices How to implement PHP security best practices May 05, 2024 am 10:51 AM

How to Implement PHP Security Best Practices PHP is one of the most popular backend web programming languages ​​used for creating dynamic and interactive websites. However, PHP code can be vulnerable to various security vulnerabilities. Implementing security best practices is critical to protecting your web applications from these threats. Input validation Input validation is a critical first step in validating user input and preventing malicious input such as SQL injection. PHP provides a variety of input validation functions, such as filter_var() and preg_match(). Example: $username=filter_var($_POST['username'],FILTER_SANIT

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

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.

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