Re: Create File using PHP
Re: Create File using PHP
And after that you will need fwrite().
Here is the example code from that page.
PHP Code:
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Re: Create File using PHP
If you use PHP 5, try this (not tested):
<html>
<head><title>Create Files</title></head>
<body>
<?php
if (isset($_POST['filename'])) {
$myfile = urlencode($_POST['filename']);
$mycontent = urlencode($_POST['filecontent']);
file_put_contents($myfile, $mycontent);
echo 'File created sucessfuly!';
}
echo '<form action="'.$_SERVER['PHP_SELF'].'">
File name: <input type="text" name="filename"><br>
File content: <input type="text" name="filecontent"><br>
<input type="submit" value="Submit">
</form>';
?>
</body>
</html>