35

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!

dferenc
  • 7,918
  • 12
  • 41
  • 49
shyamkarthick
  • 499
  • 1
  • 4
  • 17
  • i just had quite the same issue, i figured i was trying to create a file with invalid name >.<, eg $fname = $Datetime->format('Y-m-d H:i:s') like a stupid – erwan Feb 05 '18 at 20:11

4 Answers4

33

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?

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • 3
    Read the answer again. I didn't say anything about root folder. Let's assume you have a username like "bob". The web server runs as a completely different user (let's say "www"). "www" can't write "bob"'s folders, unless: 1) "www" is added to "bob"'s group and the folder has 775 permissions at least. 2) You change the owner of the folder from "bob" to "www" (using `chown`). 3) The folder is writeable by any user (it has 777 permissions, you can change them using `chmod`). – Alejandro Iván Dec 14 '13 at 06:13
  • 10
    Never ever just set your folder or file permissions to 777. See [this post](http://superuser.com/a/273533/157802) for more information. – halfpastfour.am Jul 21 '14 at 12:14
27

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.
}

?>
Cyborg
  • 1,437
  • 19
  • 40
25

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.

code-chicken
  • 349
  • 3
  • 4
0

If you want to run this command using shell, try this:

shell_exec("touch filename.filetype");

If you want to put contents in this file using shell, try this (if you do this, you do not need to touch it before this):

shell_exec("echo filecontent > filename.filetype");
Olafcio
  • 1
  • 1
  • 1