Komninos Komninos - 1 year ago 69
PHP Question

Create a file on another dir using PHP

i want to create a file inside another directory called "files".

My Server looks like this:


This is what I have tried:

$myfile = fopen("../files/file2.txt", "w")or die("Unable to open file!");

But when I run mu code it gives me this error:

Warning: fopen(../files/file2.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\createfile.php on line 4

Answer Source

If the PHP file and the folder you want to create the file in exist in the same folder, there is no need for "../". The meaning currently is that files directory is at the same level as root directory. Just remove that and it should work.

$myfile = fopen("files/file2.txt", "w")or die("Unable to open file!");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download