{"id":121,"date":"2021-03-08T00:24:03","date_gmt":"2021-03-08T00:24:03","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=121"},"modified":"2021-09-04T10:54:32","modified_gmt":"2021-09-04T10:54:32","slug":"php-file-exists","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-exists\/","title":{"rendered":"PHP File Exists"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to check if a file exists in PHP using the <code>file_exists()<\/code>, <code>is_file()<\/code>, <code>is_readable()<\/code>, and <code>is_writable()<\/code> functions.<\/p>\n\n\n\n<p>PHP provides some useful functions that allow you to check if a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-file\/\">file<\/a> exists. Let&#8217;s examine these functions and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='check-if-a-file-exists-using-the-file_exists-function'>Check if a file exists using the file_exists() function <a href=\"#check-if-a-file-exists-using-the-file_exists-function\" class=\"anchor\" id=\"check-if-a-file-exists-using-the-file_exists-function\" title=\"Anchor for Check if a file exists using the file_exists() function\">#<\/a><\/h2>\n\n\n\n<p>To check if a file exists, you use the <code>file_exist()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">file_exists ( string $filename ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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>file_exists()<\/code> function accepts a filename and returns <code>true<\/code> if the file exists; otherwise it returns <code>false<\/code>.<\/p>\n\n\n\n<p>The following example uses the <code>file_exists()<\/code> function to check if the file <code>readme.txt<\/code> exists in the current directory:<\/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>\r\n\r\n$filename = <span class=\"hljs-string\">'readme.txt'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (file_exists($filename)) {\r\n    $message = <span class=\"hljs-string\">\"The file $filename exists\"<\/span>;\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    $message = <span class=\"hljs-string\">\"The file $filename does not exist\"<\/span>;\r\n}\r\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/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<p>If the <code>readme.txt<\/code> exists in the same directory of the script, you&#8217;ll see the following message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">The<\/span> <span class=\"hljs-selector-tag\">file<\/span> <span class=\"hljs-selector-tag\">readme<\/span><span class=\"hljs-selector-class\">.txt<\/span> <span class=\"hljs-selector-tag\">exists<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230;otherwise, you&#8217;ll see a different message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">The<\/span> <span class=\"hljs-selector-tag\">file<\/span> <span class=\"hljs-selector-tag\">readme<\/span><span class=\"hljs-selector-class\">.txt<\/span> <span class=\"hljs-selector-tag\">does<\/span> <span class=\"hljs-selector-tag\">not<\/span> <span class=\"hljs-selector-tag\">exist<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">Note that the <code>$filename<\/code> can be also a path to a directory. In this case, the <code>file_exists()<\/code> function returns <code>true<\/code> if the directory exists. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='check-if-a-file-exists-using-the-is_file-function'>Check if a file exists using the is_file() function <a href=\"#check-if-a-file-exists-using-the-is_file-function\" class=\"anchor\" id=\"check-if-a-file-exists-using-the-is_file-function\" title=\"Anchor for Check if a file exists using the is_file() function\">#<\/a><\/h2>\n\n\n\n<p>If you want to check if a path is a file (not a directory) and exists in the file system, you can use the <code>is_file()<\/code> function.<\/p>\n\n\n\n<p>The <code>is_file()<\/code> function accepts a <code>$filename<\/code> and returns <code>true<\/code> if the <code>$filename<\/code> is a file and exists:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">is_file ( string $filename ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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 following example uses the <code>is_file()<\/code> function to check if the file <code>readme.txt<\/code> exists:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" 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>\r\n\r\n$filename = <span class=\"hljs-string\">'readme.txt'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (is_file($filename)) {\r\n    $message = <span class=\"hljs-string\">\"The file $filename exists\"<\/span>;\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    $message = <span class=\"hljs-string\">\"The file $filename does not exist\"<\/span>;\r\n}\r\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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='check-if-a-file-exists-and-readable'>Check if a file exists and readable <a href=\"#check-if-a-file-exists-and-readable\" class=\"anchor\" id=\"check-if-a-file-exists-and-readable\" title=\"Anchor for Check if a file exists and readable\">#<\/a><\/h2>\n\n\n\n<p>In practice, you often want to check if a file exists before reading its contents. To check if a file exists and is readable, you use the <code>is_readable()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">is_readable ( string $filename ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>is_readable()<\/code> function returns <code>true<\/code> if the <code>$filename<\/code> exists and readable, or <code>false<\/code> otherwise. Note that the <code>$filename<\/code> can be a directory.<\/p>\n\n\n\n<p>The following example uses the <code>is_readable()<\/code> function to check if the <code>readme.txt<\/code> file exists and readable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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>\r\n\r\n$filename = <span class=\"hljs-string\">'readme.txt'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (is_readable($filename)) {\r\n    $message = <span class=\"hljs-string\">\"The file $filename exists\"<\/span>;\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    $message = <span class=\"hljs-string\">\"The file $filename does not exist\"<\/span>;\r\n}\r\n<span class=\"hljs-keyword\">echo<\/span> $message;\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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='check-if-a-file-exists-and-writable'>Check if a file exists and writable <a href=\"#check-if-a-file-exists-and-writable\" class=\"anchor\" id=\"check-if-a-file-exists-and-writable\" title=\"Anchor for Check if a file exists and writable\">#<\/a><\/h2>\n\n\n\n<p>Before writing to a file, you need to check the file exists and is writable. In this case, you can use the <code>is_writable()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">is_writable ( string $filename ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>is_writable()<\/code> function returns <code>true<\/code> if the <code>$filename<\/code> exists and writable, or <code>false<\/code> otherwise.<\/p>\n\n\n\n<p>The following example uses the <code>is_writable()<\/code> function to check the <code>readme.txt<\/code> file exists and writable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" 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>\r\n\r\n$filename = <span class=\"hljs-string\">'readme.txt'<\/span>;\r\n\r\n<span class=\"hljs-keyword\">if<\/span> (is_writable($filename)) {\r\n    $message = <span class=\"hljs-string\">\"The file $filename exists\"<\/span>;\r\n} <span class=\"hljs-keyword\">else<\/span> {\r\n    $message = <span class=\"hljs-string\">\"The file $filename does not exist\"<\/span>;\r\n}\n\r\n<span class=\"hljs-keyword\">echo<\/span> $message;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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='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>file_exists()<\/code> function to check if a file exists.<\/li><li>Use the <code>is_file()<\/code> function to check if a path is a regular file, not a directory, and that file exists.<\/li><li>Use the <code>is_readable()<\/code> function to check if a file exists and readable.<\/li><li>Use the <code>is_writable()<\/code> function to check if a file exists and writable.<\/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=\"121\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-exists\/\"\n\t\t\t\tdata-post-title=\"PHP File Exists\"\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=\"121\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-file-exists\/\"\n\t\t\t\tdata-post-title=\"PHP File Exists\"\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 check if a file exists in PHP using the file_exists(), is_file(), is_readable(), and is_writable() functions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":143,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-121","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/121","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=121"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/121\/revisions"}],"predecessor-version":[{"id":2539,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/121\/revisions\/2539"}],"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=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}