<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>file creation &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/file-creation/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 23 Mar 2024 08:44:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to create a file in a different directory in PHP</title>
		<link>https://codepills.com/how-to-create-a-file-in-a-different-directory-in-php/</link>
					<comments>https://codepills.com/how-to-create-a-file-in-a-different-directory-in-php/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Sat, 05 May 2018 12:00:05 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[file creation]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://codepills.com/?p=946</guid>

					<description><![CDATA[How to make this file in another folder than the execution file location? This article will help you with a small snippet of code to create a file in a different folder. <a href="https://codepills.com/how-to-create-a-file-in-a-different-directory-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>How to make this file in another folder than the execution file location? This article will help you with a small snippet of code to create a file in a different folder.</p>
<p><span id="more-946"></span></p>
<p>First of lets have a folder structure like this:</p>
<pre><code class="language-bash">root_folder/
root_folder/index.php
root_folder/create/
root_folder/create/page.php</code></pre>
<p>Let&#8217;s imagine I want to create a file in a different folder from the folder of <code><i>php</i></code> execution file.</p>
<p>I am currently in the root folder, executing the <code><i>php</i></code> file in <code>index.php</code>. In this file, I want to run a certain function which will create me a new file in the predefined folder.</p>
<p>If you want to be sure that the folder into which you want to export the file exists, check our recent post about <a href="http://codepills.com/how-to-create-a-folder-in-php-if-it-doesnt-exist/">creating new folders</a>.</p>
<p>Here is a code that will create a new file in a predefined location.</p>
<pre><code class="language-php">$fileContent = 'This is the content in the file.';
$fileName = 'page.txt';
$filePath = './create/'.$fileName;

if (file_put_contents($filePath, $fileContent) !== false) {
    echo "File created (" . basename($filePath) . ").";
} else {
    echo "Cannot create the file (" . basename($filePath) . ").";
}</code></pre>
<h2>Conclusion</h2>
<p>This small article has shown how to create this file in a different folder rather than the execution file location.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-create-a-file-in-a-different-directory-in-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to create a folder in PHP if it doesn&#8217;t exist</title>
		<link>https://codepills.com/how-to-create-a-folder-in-php-if-it-doesnt-exist/</link>
					<comments>https://codepills.com/how-to-create-a-folder-in-php-if-it-doesnt-exist/#comments</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Fri, 04 May 2018 12:00:18 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[file creation]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://codepills.com/?p=945</guid>

					<description><![CDATA[This article will show you how to create a folder automatically in PHP if it does not exist. <a href="https://codepills.com/how-to-create-a-folder-in-php-if-it-doesnt-exist/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>This article will show you how to create a folder automatically in PHP if it does not exist. We will see several different approaches to solve this problem in the right way.</p>
<p><span id="more-945"></span></p>
<h2>Creating a new folder in PHP</h2>
<p>The most straightforward solution would be first to ask if the folder exists. If the folder does not exist, then we can create it. However, if it already exists, then we shall do nothing.</p>
<p>Here is a simple example for the custom <code>makeDirectory()</code> function:</p>
<pre><code class="language-php">&lt;?php

function makeDirectory($new_path, $mode) {
    if (!file_exists($new_path)) {
        mkdir($new_path, $mode, true);
        echo "New folder was created.&lt;br&gt;";
    }
}

$new_path = 'my_project/new/directory';
$mode = 0777;

makeDirectory($new_path, $mode); //New folder was created.
</code></pre>
<p>Let&#8217;s stop here for a while and explain a little bit used snippeds of code. The PHP <code>mkdir()</code> function will create a new directory. Its method signature according to the official PHP documentation [<a href="http://php.net/manual/en/function.mkdir.php" rel="nofollow">link</a>] is like this:</p>
<p><code>bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = FALSE [, resource $context ]]] )</code></p>
<p>As we can see only the first parameter <code>$pathname</code> is mandatory. The second parameter <code>$mode</code> is by default set to 0777. It represents default <code>chmod</code> settings for the newly created folder. For now, we only need to know that this number represents various folder access rights in operating systems.</p>
<p>If you want to know more about <code>chmod</code> , check <a href="http://codepills.com/?s=chmod">codepills.com</a> about more <code>chmod</code> articles.</p>
<p>The reason why we used the second parameter <code>chmod</code> is because we wanted to use the third method parameter <code>$recursive</code>. It allows the creation of nested directories, subdirectories, specified in the <code>$pathname</code>.</p>
<p>This code will secure creating a folder on an arbitrary path declared in the <code>$pathname</code> string from the root file where the called function is executed.</p>
<p>If you want to create a folder just in your root directory, you can write instead of the path, just the folder&#8217;s name.</p>
<h2>Creating a new folder in PHP in a defensive way</h2>
<p>A much clever and concise solution against the one we have seen is to use <code>is_dir()</code> instead of <code>file_exists()</code> snippet. <code>file_exists()</code> is method asking if the file or directory exists. <code>is_dir()</code> is optimized just for directories and therefore even faster.</p>
<p>Moreover, we should be sure that the requested path does not already exist before even asking the <code>mkdir()</code> function to do anything. Instead of if-else conditional logic, we can do boolean logic straight on the return.</p>
<pre><code class="language-php">&lt;?php

function makeDirectory($new_path, $mode) {
    return is_dir($new_path) || mkdir($new_path, $mode, true);
}

$new_path = 'my_project/new/directory';
$mode = 0777;

makeDirectory($new_path, $mode);</code></pre>
<p>The code snippet above does not work if there is already a filename with the same name. Otherwise, the code snippet will recursively create a directory path with much less code.</p>
<p>We are using the power of the <code>OR</code> operator. If the first expression is evaluated as true, it means that the folder exists. In <code>OR</code> operator first <code>true</code> evaluation is the reason to stop evaluating whole expression and evaluation do not continue further. It computer science this property of <code>OR</code> operator is better known as <b>short circuit</b>.</p>
<p>However, if the first expression is evaluated as false, <code>OR</code> will ask the second half of the expression for evaluation. Now we know that the folder does not exist, and therefore <code>mkdir()</code> method is called. If the<code>mkdir()</code> function creates the folder without error, it will return true. Therefore, in the end, we get confirmation that the new folder exists.</p>
<pre><code class="language-php">&lt;?php

function makeDirectory($new_path, $mode) {
    $return = mkdir($new_path, $mode, true);
    return $return === true || is_dir($new_path);
}

$new_path = 'my_project/./directory';
$mode = 0777;

$result = makeDirectory($new_path, $mode);
echo($result);
</code></pre>
<p>If we are sure that the directory we want to create will not exist, we can slightly change our custom function and make it in the same way more effective.</p>
<p>But <b>be aware of forbidden characters</b>. <u>Each operating system has some special characters which can not be used for the directory name.</u> For example, Windows operating systems denies using <code>/?&lt;&gt;\:*|"</code> , Unix based systems deny using dot in the folder name.</p>
<h2>Conclusion</h2>
<p>This article has shown how to create a folder automatically in PHP if it does not exist. We have demonstrated several different approaches to solve this problem in the right way.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-create-a-folder-in-php-if-it-doesnt-exist/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
