Reputation: 179
How would I create a file in a specific directory?
this is my code at the moment :
$username = $_POST["username"];
$filedir = "./u/".$username;
mkdir($filedir);
$folder = $filedir;
chmod($filedir, 0777);
$createfile = fopen( $_SERVER['DOCUMENT_ROOT'] . '/path/filename.php' );
I HAVE GOT IT!!! It was
$createfile = fopen('./u/'.$username.'/'.$username.'.php', 'x');
Upvotes: 6
Views: 23013
Reputation: 551
I wanna make it more obvious for those guys that came here for :
"How to create a file in a specific Address in PHP"
function createFile($name, $address){
$createfile = fopen($address.'/'.$name, 'x');
}
That's it . This function will take the address and the name of your file (whatever you wanna call it), and it will make it for you in that exact address
ATTENTION:
For those who think it is the same as the answer of the other guy , so it's not , because i had error and problem with the answer so i tried and split the address
and the filename
and i fixed my problem
Upvotes: 0
Reputation: 179
Answer was this :
$createfile = fopen('./u/'.$username.'/'.$username.'.php', 'x');
Upvotes: 4
Reputation: 33813
You could try with these functions - one recursively creates a folder path and the other will create an empty file in the designated path.
/* recursively create folder path */
function createdir( $path=null, $perm=0644 ) {
if( !file_exists( $path ) ) {
createdir( dirname( $path ) );
mkdir( $path, $perm, true );
clearstatcache();
}
}
/* create an empty file ensuring that path is constructed */
function createfile( $path=false, $filename=false ){
if( $path && $filename ){
createdir( $path );
file_put_contents( $path . DIRECTORY_SEPARATOR . $filename, '' );
return true;
}
return false;
}
$dir=$_SERVER['DOCUMENT_ROOT'] . '/path/to/dir';
$filename='geronimo.txt';
$result=call_user_func('createfile', $dir, $filename );
Upvotes: 0
Reputation: 10695
mode Required. Specifies the type of access you require to the file/stream.
fopen(filename,mode)
$createfile = fopen($_SERVER['DOCUMENT_ROOT'] . '/path/filename.php',"r"); // read only
Possible values:
Upvotes: 3