tags:

views:

48

answers:

4
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

The file "testFile.txt" should be created in the same directory where this PHP code resides.Now is there any method to create this file in some other folder. I tried something like this -

$ourFileName = "http://abc.com/folder1/folder2/testFile.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

But it did not work.

+3  A: 

Now is there any method to create this file in some other folder.

Sure. You just need to specify a filesystem path, not a URL.

$ourFileName = "/path/to/myfile/test.txt";
$ourFileName = "../myfile/test.txt";
Pekka
+3  A: 

testfile.txt will be placed in the same folder.

Just as ./testfile.txt. If you want to use relative path you can do something like:

./../../testfile.txt which will be 2 levels up in the directory tree.

You can also use absolute path in order to select the directory: /root/somefolder/inner/file.txt

galambalazs
+4  A: 

use .. to go up one level

for example: $ourFileName = "../folder1/testFile.txt";

dejavu
+3  A: 

Don't use an URL, use a local path. Like $ourFileName = 'folder1/folder2/testFile.txt';

Understand this - when the PHP code is executed on the server it's no different than any other program which is executed locally on the server. That means it has some process under which it runs, that process has credentials with which it is executing, command line, etc. And also the current path, which is set by PHP to be the same as the file which is being requested by the browser.

Vilx-