0

So this is my code:

<?php
$path = 'logs/' . date("Y:m:d") . '.txt';

$get = $_POST['data']; // I know this is insecure, don't worry
$file = fopen($path, 'w') or die("Something is wrong with your file permissions, please obtain the nessecary rights!");
fwrite($file, $get) or die('error writing to file');    
?>

the fopen() function works fine, I don't get any error whatsoever. The problem is that I get an "error writing to file" meaning there is something wrong with my fwrite() function.

What could be the problem here?

I'm using apache2 and php7.

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
David
  • 49
  • 7

1 Answers1

3

It wouldn't work because of the name of your file name as in:

$path = 'logs/' . date("Y:m:d") . '.txt';

As you will notice, your generated path is going to be something like: logs/2016:12:18.txt implying that the name of your .txt file is 2016:12:18 which isn't a valid name as a file name CANNOT contain a colon (:).

Please note; the following characters cannot be used when naming files:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (forward slash)
  • \ (backslash)
  • | (vertical bar or pipe)
  • ? (question mark)
  • * (asterisk)

... read more here and here.

You should amend to an acceptable file name so as to get it working.

Below is an example, using hyphen (-) as delimiter:

$path = 'logs/' . date("Y-m-d") . '.txt'; // Expected result: logs/2016-12-18.txt
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
  • You can on non-windows systems right? Actually, the file got created just fine. The problem was that I forgot i needed to use a POST request. Whoops. I'll accept this answer. – David Dec 18 '16 at 20:33
  • @David: You may find [this](http://stackoverflow.com/q/1976007/6381711) post helpful as well. – nyedidikeke Dec 18 '16 at 22:33