Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php create a file if not exists

I try to create files and write the contents dynamically. Below is my code.

$sites = realpath(dirname(__FILE__)).'/';
$newfile = $sites.$filnme_epub.".js";

if (file_exists($newfile)) {
    $fh = fopen($newfile, 'a');
    fwrite($fh, 'd');
} else {
    echo "sfaf";
    $fh = fopen($newfile, 'wb');
    fwrite($fh, 'd');
}

fclose($fh);
chmod($newfile, 0777);

// echo (is_writable($filnme_epub.".js")) ? 'writable' : 'not writable';
echo (is_readable($filnme_epub.".js")) ? 'readable' : 'not readable';
die;

However, it does not create the files.

Please share your answers and help. Thanks!

like image 495
shyamkarthick Avatar asked Dec 14 '13 05:12

shyamkarthick


People also ask

How create file if not exists in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do you check if a file already exists in PHP?

The file_exists() function checks whether a file or directory exists.

What is $_ files in PHP?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.


3 Answers

Try using:

$fh = fopen($newfile, 'w') or die("Can't create file");

for testing if you can create a file there or not.

If you can't create the file, that's probably because the directory is not writeable by the web server user (usually "www" or similar).

Do a chmod 777 folder to the folder you want to create the file and try again.

Does it work?

like image 198
Alejandro Iván Avatar answered Oct 17 '22 00:10

Alejandro Iván


Use the function is_file to check if the file exists or not.

If the file doesn't exist, this sample will create a new file and add some contents:

<?php

$file = 'test.txt';

if(!is_file($file)){
    $contents = 'This is a test!';           // Some simple example content.
    file_put_contents($file, $contents);     // Save our content to the file.
}

?>
like image 40
Cyborg Avatar answered Oct 16 '22 23:10

Cyborg


To be sure that a file exists before doing anything with it you can just touch it:

if (!file_exists('somefile.txt')) {
    touch('somefile.txt');
}

This will just create an empty file having the current time as creation time. The advantage over fopen is that you don't have to close the file.

You can also set the creation time, if you want. The following code will create a file with a creation time of yesterday:

if (!file_exists('somefile.txt')) {
    touch('somefile.txt', strtotime('-1 days'));
}

However: You should care about the fact that a file's modification time will be changed if you use touch for files which already exists.

like image 18
code-chicken Avatar answered Oct 17 '22 00:10

code-chicken