I'm executing a php file from the php-cli with a shell script. One of the php files creates a file named available_data during the course of its execution. The problem is that the php file needs to write the file available_data to the same directory where the php file resides on the server /var/www/html. Instead it writes the file to the directory where the shell script resides /bin/scripts/.

Here's my script (resides in /bin/scripts/createview.sh)

php /var/www/html/createview_customer.php

I think the problem is that the php file executes in /bin/scripts. How can I get the php file to execute in /var/www/html instead?

1 Answer 1


Modify your shell script to enter the php scripts directory before executing it, and then return back.

cd /var/www/html && php ./createview_customer.php
cd "$cur"

or better modify your php script to find its directory first and open the file in that dir

$dirpath = dirname(__FILE__);
$handle = fopen("$dirpath/available_data", "w");
  • 1
    A better way of doing the first method is to localize the directory change in a subshell: (cd /var/www/html && php ./createview_customer.php). Apr 8, 2011 at 19:33
  • Yes, I thought of that, but since I didn't know the context I chose not to run it in a subshell.
    – forcefsck
    Apr 8, 2011 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .