meme
meme

Reputation: 179

php how to create file in specific directory

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 also tried fopen but that didn't work :(

I HAVE GOT IT!!! It was

$createfile = fopen('./u/'.$username.'/'.$username.'.php', 'x');

Thanks for all the help guy :) really appreciated.

Upvotes: 6

Views: 23013

Answers (4)

Hosseinreza
Hosseinreza

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

meme
meme

Reputation: 179

Answer was this :

$createfile = fopen('./u/'.$username.'/'.$username.'.php', 'x');

Thanks for all the help guy :) really appreciated.

Upvotes: 4

Professor Abronsius
Professor Abronsius

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

Kushan
Kushan

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:

  • "r" (Read only. Starts at the beginning of the file)
  • "r+" (Read/Write. Starts at the beginning of the file)
  • "w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
  • "w+" (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist)
  • "a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)
  • "a+" (Read/Write. Preserves file content by writing to the end of the file)
  • "x" (Write only. Creates a new file. Returns FALSE and an error if file already exists)
  • "x+" (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)

Upvotes: 3

Related Questions