{"id":115,"date":"2021-03-08T00:22:56","date_gmt":"2021-03-08T00:22:56","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=115"},"modified":"2025-04-08T04:18:50","modified_gmt":"2025-04-08T04:18:50","slug":"php-create-file","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-create-file\/","title":{"rendered":"PHP Create File"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn a couple of ways to create a new file in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-file-using-the-fopen-function'>Creating a file using the fopen() function <a href=\"#creating-a-file-using-the-fopen-function\" class=\"anchor\" id=\"creating-a-file-using-the-fopen-function\" title=\"Anchor for Creating a file using the fopen() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>fopen()<\/code> function <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-open-file\/\">opens a file<\/a>. It also creates a file if the file doesn&#8217;t exist. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>fopen()<\/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\">fopen ( \n   string $filename , \n   string $mode , \n   bool $use_include_path = <span class=\"hljs-keyword\">false<\/span> , \n   resource $context = ? \n) : resource<\/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>To create a new file using the <code>fopen()<\/code> function, you specify the <code>$filename<\/code> and one of the following modes:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Mode<\/th><th>File Pointer<\/th><\/tr><\/thead><tbody><tr><td>&#8216;w+&#8217;<\/td><td>At the beginning of the file<\/td><\/tr><tr><td>&#8216;a&#8217;<\/td><td>At the end of the file<\/td><\/tr><tr><td>&#8216;a+&#8217;<\/td><td>At the end of the file<\/td><\/tr><tr><td>&#8216;x&#8217;<\/td><td>At the beginning of the file<\/td><\/tr><tr><td>&#8216;x+&#8217;<\/td><td>At the beginning of the file<\/td><\/tr><tr><td>&#8216;c&#8217;<\/td><td>At the beginning of the file<\/td><\/tr><tr><td>&#8216;c+&#8217;<\/td><td>At the beginning of the file<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Except for the <code>'a'<\/code> and <code>'a+'<\/code>, the file pointer is placed at the beginning of the file. <\/p>\n\n\n\n<p>If you want to create a binary file, you can append the character <code>'b'<\/code> to the <code>$mode<\/code> argument. For example, the <code>'wb+'<\/code> opens a binary file for writing.<\/p>\n\n\n\n<p>The following example uses <code>fopen()<\/code> to create a new binary file and write some numbers to it:<\/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$numbers = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>];\n$filename = <span class=\"hljs-string\">'numbers.dat'<\/span>;\n\n$f = fopen($filename, <span class=\"hljs-string\">'wb'<\/span>);\n<span class=\"hljs-keyword\">if<\/span> (!$f) {\n    <span class=\"hljs-keyword\">die<\/span>(<span class=\"hljs-string\">'Error creating the file '<\/span> . $filename);\n}\n\n<span class=\"hljs-keyword\">foreach<\/span> ($numbers <span class=\"hljs-keyword\">as<\/span> $number) {\n    fputs($f, $number);\n}\n\nfclose($f);<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define an array of five numbers from 1 to 5.<\/li>\n\n\n\n<li>Second, use the <code>fopen()<\/code> to create the <code>numbers.dat<\/code> file.<\/li>\n\n\n\n<li>Third, use the <code>fputs()<\/code> function to write <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-foreach\/\">each<\/a> number in the <code>$numbers<\/code> array to the file.<\/li>\n\n\n\n<li>Finally, close the file using the <code>fclose()<\/code> function.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-file-using-the-file_put_contents-function'>Creating a file using the file_put_contents() function <a href=\"#creating-a-file-using-the-file_put_contents-function\" class=\"anchor\" id=\"creating-a-file-using-the-file_put_contents-function\" title=\"Anchor for Creating a file using the file_put_contents() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>file_put_contents()<\/code> function writes data into a file. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>file_put_contents()<\/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\">file_put_contents ( \n    string $filename , \n    mixed $data , \n    int $flags = <span class=\"hljs-number\">0<\/span> , \n    resource $context = ? \n) : int<\/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>If the file specified by the <code>$filename<\/code> doesn&#8217;t exist, the function creates the file.<\/p>\n\n\n\n<p>The <code>file_put_contents()<\/code> function is identical to calling the <code>fopen()<\/code>, <code>fputs()<\/code>, and <code>fclose()<\/code> functions successively to write data to a file.<\/p>\n\n\n\n<p>The following example downloads a webpage using the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-file_get_contents\/\">file_get_contents()<\/a><\/code> function and write HTML to a 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\n$url = <span class=\"hljs-string\">'https:\/\/www.php.net'<\/span>;\n$html = file_get_contents($url);\nfile_put_contents(<span class=\"hljs-string\">'home.html'<\/span>, $html);<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, download a webpage <code>https:\/\/www.php.net<\/code> using the <code>file_get_contents()<\/code> function.<\/li>\n\n\n\n<li>Second, write the HTML to the <code>home.html<\/code> file using the <code>file_put_contents()<\/code> function<\/li>\n<\/ul>\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\">\n<li>Use the <code>fopen()<\/code> function with one of the mode <code>w<\/code>, <code>w+<\/code>, <code>a<\/code>, <code>a+<\/code>, <code>x<\/code>, <code>x+<\/code>, <code>c<\/code>, <code>c+<\/code> to create a new file.<\/li>\n\n\n\n<li>Use the <code>file_put_contents()<\/code> function to create a file and write data to it.<\/li>\n\n\n\n<li>The <code>file_put_contents()<\/code> function is identical to calling <code>fopen()<\/code>, <code>fputs()<\/code>, and <code>fclose()<\/code> functions successively to write data to a file.<\/li>\n<\/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=\"115\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-create-file\/\"\n\t\t\t\tdata-post-title=\"PHP Create File\"\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=\"115\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-create-file\/\"\n\t\t\t\tdata-post-title=\"PHP Create File\"\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 create a file in PHP using the fopen() and file_put_contents() functions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":146,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-115","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/115","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=115"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/115\/revisions"}],"predecessor-version":[{"id":3296,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/115\/revisions\/3296"}],"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=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}