1

I want to create a file failure-log.log in a specific directory and write to it. Already got the directory path from database. The path is like this:

D:/folder-one/folder-two/

And my PHP code is executing in another directory like this:

C:/apache24/crawler/admin/startService.php

How can I create the file and write to it?

shsh
  • 51
  • 1
  • 7
  • I notice that you want to write a file outside web root, which is *not* suggested due to security concern. If you insist, you must turn off [safe_mode](http://nl2.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode) or leave [doc_root](http://nl2.php.net/manual/en/ini.core.php#ini.doc-root) to be empty in php.ini. Even you performed the above, you still need to give proper permission. – Raptor Aug 09 '18 at 07:11
  • @Raptor Thanks for making me aware of this – shsh Aug 09 '18 at 07:30

4 Answers4

1

Use file_put_contents method, like this:

$file ="D:/folder-one/folder-two/";
$current = file_get_contents($file); 
$current .= 'yourcontenthere';
file_put_contents($file, $current);

U can send flags to file_put_contents, like FILE_APPEND :

$file ="D:/folder-one/folder-two/"; 
$text = 'yourcontenthere';
file_put_contents($file, $text, FILE_APPEND);

In that case u will not have to retrieve the old content, u can check and other flags in link above.

Also is good idea to check if file exists before that.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
1

Make sure you use absolute path(you can use realpath() on relative path as well to be sure about the path) and directory is writable

then

$dir = 'your/path';

file_put_contents($dir ."/failure-log.log", $contentOfFile);

if you want the content of file not to be removed everytime then I suggest using FILE_APPEND

file_put_contents($dir ."/failure-log.log", $contentOfFile, FILE_APPEND);
Robert
  • 19,800
  • 5
  • 55
  • 85
0

Make that folder location writable for web server first. Then use below code to create file in that location.

$myfile = fopen("D:/folder-one/folder-two/file.log", "a") or die("Unable to open location for log file !");
$txt = "Log details goes here ...";
fwrite($myfile, $txt);
fclose($myfile);
LahiruTM
  • 638
  • 4
  • 16
-1
  1. Write to files

    $writeFile = @fopen('/path/to/save/file', 'w+'); @fwrite($writeFile, $content); @fclose($writeFile);

With:

w+: will create a new file if it does not exist and overwrite if it exists
a: append to file already exist
a+: append to file already exist and create a new file if it does not exist
  1. If you load path directory from the database, you maybe need to create multi-directory

    if( !is_dir($path) ) { mkdir($path, 0777, true); }

With:

$path: path you were loaded from db
ThangLe
  • 878
  • 1
  • 8
  • 15
  • Never assign 777 permission to any folder or file; it is a common mistake, which will lead to security issues. If you want to create a folder with write permission for scripts, you use should 775 instead. – Raptor Aug 09 '18 at 08:19
  • Why do you downvote me? @Raptor – ThangLe Aug 09 '18 at 15:18
  • Mentioned in the previous comment. – Raptor Aug 10 '18 at 02:26
  • 1
    @Raptor I'm using the window OS, actually, I don't care your "lead to security issues" because mode permission doesn't works on Window. Additional, mode permission of mkdir default is 0777. You can check here http://php.net/manual/en/function.mkdir.php. – ThangLe Aug 22 '18 at 09:58