0

I have a problem output my log file to my Desktop. I am using Windows 7.

<?php 

        $log = "All the log file !";
        $file_name = 'data.txt';
        $file_path = 'Desktop\\test\\'.$file_name;
        file_put_contents($file_path, $log );

On my Desktop already have a test folder.

enter image description here

when I open it, nothing is there

enter image description here

What did I do wrong - here ?

3 Answers3

2

First, you desktop folder is located at:

C:\Users\{username}\Desktop 

Second you can try to check the result of the file_put_contents function.

Third, if you have any problem, first turn on the debug/test mode of your Framework, or if you are not using one just add this on top of your file:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

For easier:

$file_path = 'C:\\Users\\bheng\\Desktop\\test\\'.$file_name;
Lachezar Todorov
  • 762
  • 7
  • 20
  • I did `$file_path = 'C:\\Users\\bheng\\Desktop\\test'.$file_name;`, still didn't see anything output ! –  Feb 13 '15 at 14:51
  • Did you checked this path in Explorer? – Lachezar Todorov Feb 13 '15 at 14:52
  • 1
    @kula That code will create file on desktop not inside `test` this is correct `'C:\\Users\\bheng\\Desktop\\test\\'.$file_name;` – VeeeneX Feb 13 '15 at 14:53
  • This is exact path in Explorer = `C:\Users\bheng\Desktop\test` - and I see nothing there ! when I refresh the page ! –  Feb 13 '15 at 14:54
  • Good move to turn on error reporting. It's always tough to tell with code fragments, but the missing ";" on the line: file_put_contents($file_path, $log ) may be a problem. @Bogdan Kuštan I can see you've corrected that. – BigScar Feb 13 '15 at 15:00
  • @BigScar : Good called, I caught that as well, but that is not the problem here. I fixed that, and nothing generated. –  Feb 13 '15 at 15:02
  • @LachezarTodorov : I throw that 2 lines that you suggest on the top of my php file, and I don't any error showing. At the same time file is still not export . –  Feb 13 '15 at 15:03
  • @LachezarTodorov : Not I got sth different. `Warning: file_put_contents(C:\Users\bheng\Desktop\test\data.txt): failed to open stream: Permission denied in C:\Inetpub\wwwroot\php\index.php on line 7` –  Feb 13 '15 at 15:09
  • That line 7 is this line = `file_put_contents( 'C:\\Users\\bheng\\Desktop\\test\\data.txt', $log );` –  Feb 13 '15 at 15:09
  • Well that being said, do I need to adjust and permission level on my `test` folder ? –  Feb 13 '15 at 15:10
  • So you have to change the folder permissions. – Lachezar Todorov Feb 15 '15 at 08:44
  • Maybe this will help: http://serverfault.com/questions/159902/change-permissions-in-window-7-so-php-can-write-files – Lachezar Todorov Feb 15 '15 at 08:48
  • Are you using wamp, other *amp or custom installation – Lachezar Todorov Feb 15 '15 at 08:57
0

file path in windows to desktop is C:\Users\YOUR_USERNAME\Desktop. So You need to adjust path, or use $_SERVER['HOME'] variable if you are running from CLI.

$log = "All the log file !";
$file_name = 'data.txt';
$file_path = $_SERVER['HOME'] . '\\' . 'Desktop\\test\\'.$file_name;
file_put_contents($file_path, $log );

Or generate path to desktop:

echo exec('echo %SystemDrive%') . '\\Users\\' . get_current_user() . '\\Desktop\\Test';

Becauso You are using IIS, PHP runs as different user. Su You bus hardcode Your path:

$file_path = 'C:\\Users\\Bogdan\\Desktop\\Test\\' . $file_name;
Bogdan Kuštan
  • 4,901
  • 1
  • 16
  • 29
  • are you using CLI? Bogdan wrote: "if you are running from CLI" – Lachezar Todorov Feb 13 '15 at 14:59
  • No - I didn't. I used IIS in wondows 7 to load/run/display my php code. –  Feb 13 '15 at 15:04
  • @Bogdan - you're right, now I got a different error message saying that `Warning: file_put_contents(C:\Users\bheng\Desktop\test\data.txt): failed to open stream: Permission denied` –  Feb 13 '15 at 15:12
  • How to do I fixed this issue ? Is there a way to force save it ? –  Feb 13 '15 at 15:13
  • @kula You need to set permissions to write to desktop folder. Firstly get username which PHP is running with `get_current_user()` and then in folder properties security tab, allow that user write permissions. – Bogdan Kuštan Feb 13 '15 at 15:16
  • @BogdanKuštan : I did this `echo get_current_user();` - and I got wired return `IUSR ` - I don't know who is IUSR !!! –  Feb 13 '15 at 15:19
  • @kula `IUSR` is Your IIS user as which PHP is running. You need to allow write permissions on Your `test` folder to `IUSR` user, more about permissions could be found [in this post](http://stackoverflow.com/questions/14934006/iis-iusrs-and-iusr-permissions-in-iis8) – Bogdan Kuštan Feb 17 '15 at 14:47
-1

You cannot write directly to the user's file system.

what you need to do is use php to trigger the user to download the file you're trying to deliver.

http://php.net/manual/en/function.readfile.php

jdu
  • 571
  • 2
  • 3