PHP - Create file if it doesn’t already exist
source link: https://www.laravelcode.com/post/php-create-file-if-it-doesn-t-already-exist
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
PHP - Create file if it doesn’t already exist
In this article, we will use PHP to check if a file exists before we strive to create it. that is specially beneficial in case you simplest need create the file as soon as (for file caching functions, and so forth). i.e. You need to avoid any unnecessary writes to the disk.
In this guide, we will be creating a simple text file called test.txt...
test.txt
//The name of the file that we want to create if it doesn't exist.
$file = 'test.txt';
//Use the function is_file to check if the file already exists or not.
if(!is_file($file)){
//Some simple example content.
$contents = 'This is a test!';
//Save our content to the file.
file_put_contents($file, $contents);
}
In the above PHP test.txt file...
1. We created a variable called $file
. This variable contains the name of the file that we want to create.
2. Using PHP’s is_file
function, we check to see if the file already exists or not.
3. If is_file
returns a boolean FALSE value, then our filename does not exist.
4. If the file does not exist, we create the file using the function file_put_contents
.
Author : Harsukh Makwana
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
24
H ow do I allow Linux processes to bind to IP address that doesn’t exist yet on my Linux systems or server? You need to set up net.ipv4.ip_nonlocal_bind , which allows processes to bind() to non...
-
6
Sometimes when writing Swift code you want to reference an instance before its creation. It's not a very common, which is why I'm writing this post so I can easily remember it next time it happens.To have an example to work with, let's...
-
5
Uncoupled code is good, but doesn't exist Wed, Nov 19, 2008Code should try to be as decoupled from the code it depends as possible, I want me C++ to work with any compiler, I want my web framework to work with any ORM, I want...
-
5
The page you were looking for doesn't exist (404) This page does not exist Return to Home Page
-
8
Executive Checklist5 Ways to Build A Successful Business in an Industry That Doesn’t ExistAdam Olalde, CEO & Founder at Xtreme Xperience
-
4
Company comments on Android plans in lawsuit docs iMessage is one rather popular messaging platform, but unfortunately for almost everybody, it’s only available on Apple’s ecosystem, so technically,...
-
9
The $149 Smartphone That Could Bring The Linux Mobile Ecosystem to LifeThe low-cost PinePhone isn’t a perfect device, but the nuanced ecosystem it’s going to build for Linux-based smartphones is going to be ama...
-
8
Face palm: When the emoji you want doesn't existBy Sarah Treanor and Vivienne NunisBusiness reporters Published2 days agoimage copyrightGetty Imagesimage captionFor man...
-
4
PHP File /tmp/ Does Not Exist Jan 23, 2017 · 2 minute read Category: PHP Recently I ran into an issue with a upload issu...
-
4
The Next Great Overdose-Reversing Drug Might Already ExistFentanyl-related substances have a bad reputation, but they could also save lives. In the US, a legislative battle to expedite research is heati...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK