Bruno Andrade
Bruno Andrade

Reputation: 595

PHP file path from Command Line different

If I run this simple code through the browser, it works normally and creates 'text.txt' in the same location as the script.

 <?php

    $op = fopen('text.txt', 'w');

    fwrite($op, 'test');

    fclose($op)

    ?>

But if I run with command line

php script.php

it creates the file in a completely different directory.

Why does the same script behave differently by running the browser and the command line ?!

To fix this I need to put the full path: fopen('/home/user/site.com/subfolder/text.txt', 'w');

I have a script with many lines and many file paths, it would be too much work to change all. How can I fix this problem without putting the complete path in the file.

Upvotes: 2

Views: 714

Answers (2)

Mike Q
Mike Q

Reputation: 7327

start_script.sh

#!/bin/bash
cd /to/location/of/script
php script.php

Upvotes: 0

Salim Ibrohimi
Salim Ibrohimi

Reputation: 1391

Open Terminal and type:

cd /home/user/site.com/subfolder

then run:

php script.php

Upvotes: 1

Related Questions