Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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");
share|improve this answer
+1 for multiple options. Both work well. Thank you. – JMC Apr 8 '11 at 17:45
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). – Gilles Apr 8 '11 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 '11 at 19:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.