Komninos Komninos - 1 month ago 7
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:

root:
createfile.php
files:
file1.txt


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

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!");
Comments