ayush
ayush

Reputation: 14568

Create files in PHP in some other folder

$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.

Upvotes: 0

Views: 11358

Answers (4)

Vilx-
Vilx-

Reputation: 106904

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.

Upvotes: 3

Ahmed Aman
Ahmed Aman

Reputation: 2393

use .. to go up one level

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

Upvotes: 3

gblazex
gblazex

Reputation: 50111

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

Upvotes: 3

Pekka
Pekka

Reputation: 449385

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";

Upvotes: 5

Related Questions