<span><span><span class="hljs-meta"><?php</span></span><span>
// This part is unrelated to the article content and can include some initialization code
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span>('Asia/Shanghai');
</span><span><span class="hljs-variable">$logFile</span> = </span><span><span class="hljs-keyword">__DIR__</span> . '/log.txt';
</span><span><span class="hljs-keyword">if</span> (!<span class="hljs-title function_ invoke__">file_exists</span>($logFile)) {
<span class="hljs-title function_ invoke__">file_put_contents</span>($logFile, "Log initialized\n");
}
</span><span><span class="hljs-variable">$timestamp</span> = <span class="hljs-title function_ invoke__">date</span>('Y-m-d H:i:s');
</span><span><span class="hljs-title function_ invoke__">file_put_contents</span>($logFile, "Script run time: <span class="hljs-subst">$timestamp</span>\n", FILE_APPEND);
</span><span><span class="hljs-meta">?></span>
<p><hr></p>
<p></span>/**</p>
<ul>
<li>
<p>Title: How to Use the touch() Function with chmod() to Adjust File Permissions?</p>
</li>
<li></li>
<li>
<p>In PHP, the touch() function is commonly used to create a new file or update the modification time of an existing file,</p>
</li>
<li>
<p>while the chmod() function is used to set file access permissions. Using them together allows you to</p>
</li>
<li>
<p>conveniently control the file’s permissions when creating it, ensuring it can be read and written correctly in your application.</p>
</li>
<li></li>
<li>
<p>Example steps are as follows:<br>
*/</p>
</li>
</ul>
<p>// 1. Create a file using touch()<br>
$filePath = <strong>DIR</strong> . '/example.txt';<br>
if (touch($filePath)) {<br>
echo "File created or modification time updated: $filePath\n";<br>
} else {<br>
echo "Failed to create file\n";<br>
}</p>
<p>// 2. Set file permissions using chmod()<br>
// For example, set to 0644, meaning owner can read/write, group and others can read<br>
if (chmod($filePath, 0644)) {<br>
echo "File permissions set to 0644\n";<br>
} else {<br>
echo "Failed to set file permissions\n";<br>
}</p>
<p>/**</p>
<ul>
<li>
<p>3. Notes:</p>
</li>
<li></li>
<li>
<ul>
<li>
<p>The permission parameter for chmod() is usually expressed in octal, such as 0644 or 0755.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>Actual permission enforcement depends on the operating system and the user PHP runs as.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>On some systems, if the PHP process lacks sufficient privileges, chmod() may fail.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>touch() can also update the access and modification times of existing files, not just create files.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Using touch() together with chmod() makes it easy to initialize files such as log files,</p>
</li>
<li>
<p>temporary files, or configuration files, ensuring their permissions comply with security policies.<br>
*/<br>
?></p>
</li>
</ul>
<p data-is-last-node="" data-is-only-node="">// This part is also unrelated to the article content and can include script-ending logs or cleanup<br>
file_put_contents($logFile, "Script execution completed\n", FILE_APPEND);<br>
<span class="hljs-meta">?>
Related Tags:
array_values