{"id":124,"date":"2021-03-08T00:24:45","date_gmt":"2021-03-08T00:24:45","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=124"},"modified":"2021-04-19T06:59:19","modified_gmt":"2021-04-19T06:59:19","slug":"php-file-permissions","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-permissions\/","title":{"rendered":"PHP File Permissions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to deal with PHP file permissions<strong>,<\/strong> including checking and changing file permissions.<\/p>\n\n\n\n<p>File permissions specify what a user can do with a file, e.g., reading, writing, or executing it. Notice that PHP automatically&nbsp;grants appropriate permissions behind the scenes. <\/p>\n\n\n\n<p>For example, if you&nbsp;<a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-create-file\/\">create a new file<\/a> for writing, PHP automatically grants the read and write permissions. <\/p>\n\n\n\n<p>PHP provides some useful functions for checking and changing the file permissions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='checking-file-permissions'>Checking file permissions <a href=\"#checking-file-permissions\" class=\"anchor\" id=\"checking-file-permissions\" title=\"Anchor for Checking file permissions\">#<\/a><\/h2>\n\n\n\n<p>PHP has three handy functions that check file permissions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>is_readable()<\/code> function returns <code>true<\/code> if the file exists and is readable; otherwise, it returns <code>false<\/code>.<\/li><li><code>is_writable()<\/code> function returns <code>true<\/code> if the file exists and is writable; otherwise, it returns <code>false<\/code>.<\/li><li><code>is_executable()<\/code> function returns <code>true<\/code> if the file exists and executable; otherwise, it returns <code>false<\/code>.<\/li><\/ul>\n\n\n\n<p>Let&#8217;s take a look at the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$filename = <span class=\"hljs-string\">'readme.txt'<\/span>;\n\n$functions = &#91;\n\t<span class=\"hljs-string\">'is_readable'<\/span>,\n\t<span class=\"hljs-string\">'is_writable'<\/span>,\n\t<span class=\"hljs-string\">'is_executable'<\/span>\n];\n\n<span class=\"hljs-keyword\">foreach<\/span> ($functions <span class=\"hljs-keyword\">as<\/span> $f) {\n\t<span class=\"hljs-keyword\">echo<\/span> $f($filename) ? <span class=\"hljs-string\">'The file '<\/span> . $filename . $f : <span class=\"hljs-string\">''<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Besides those functions, PHP also provides the <code>fileperms()<\/code> function that returns an integer, which represents the permissions set on a particular file. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$permissions = fileperms(<span class=\"hljs-string\">'readme.txt'<\/span>);\n\n<span class=\"hljs-keyword\">echo<\/span> substr(sprintf(<span class=\"hljs-string\">'%o'<\/span>, $permissions), <span class=\"hljs-number\">-4<\/span>); <span class=\"hljs-comment\">\/\/0666<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='changing-file-permissions'>Changing file permissions <a href=\"#changing-file-permissions\" class=\"anchor\" id=\"changing-file-permissions\" title=\"Anchor for Changing file permissions\">#<\/a><\/h2>\n\n\n\n<p>To change the file permission or mode, you use the <code>chmod()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">chmod ( string $filename , int $permissions ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>chmod()<\/code> function has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>$filename<\/code> is the file that you want to change the permissions.<\/li><li>$permissions parameter consists of three octal number components that specify access restrictions for the owner, the user group in which the owner is in, and everyone else in this sequence.<\/li><\/ul>\n\n\n\n<p>The <code>chmod()<\/code> function returns <code>true<\/code> on success or <code>false<\/code> on failure.<\/p>\n\n\n\n<p>The permissions argument is represented by an octal number that contains three digits:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first digit specifies what the owner of the file can read, write, or execute the file.<\/li><li>The second digit specifies what the user group in which the owner is in can read, write, or execute the file.<\/li><li>The third digit specifies what everyone else can read, write, or execute the file.<\/li><\/ul>\n\n\n\n<p>The following table illustrates the value of each digit that represents the access permission for particular users ( owner, user group, or everyone else) :<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Value<\/th><th>Permission<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>cannot read, write or execute<\/td><\/tr><tr><td>1<\/td><td>can only execute<\/td><\/tr><tr><td>2<\/td><td>can only write<\/td><\/tr><tr><td>3<\/td><td>can write and execute<\/td><\/tr><tr><td>4<\/td><td>can only read<\/td><\/tr><tr><td>5<\/td><td>can read and execute<\/td><\/tr><tr><td>6<\/td><td>can read and write<\/td><\/tr><tr><td>7<\/td><td>can read, write and execute<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example sets permission that the only owner can read and write a file, everyone else only can read the file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n$filename = <span class=\"hljs-string\">'.\/readme.txt'<\/span>;\nchmod($filename, <span class=\"hljs-number\">0644<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that we put <code>0<\/code> before <code>644<\/code> to instruct PHP to treat it as an octal number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>is_readable()<\/code>, <code>is_writable()<\/code>, <code>is_executable()<\/code> to check if a file exists and readable, writable, and executable.<\/li><li>Use the <code>chmod()<\/code> function to set permissions for a file.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-permissions\/\"\n\t\t\t\tdata-post-title=\"PHP File Permissions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"124\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-permissions\/\"\n\t\t\t\tdata-post-title=\"PHP File Permissions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the PHP chmod() function to set the permission for a file.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":154,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-124","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":8,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/124\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/124\/revisions\/1245"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}