PHP Create and Save a txt file to root directory
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
html/php create and save text file to root directory with custom filename
HTML can't create files.
Assuming you have a single file (let's call it “index.php”), here is an example of code necessary to deal with both the server-side php code and the client-side html code.
<?php
if( $_POST["name"] || $_POST["data"] ) {
$name = $_POST['name'];
$data = $_POST['data'];
if(file_exists($name)){
// Do something if file already exists
}else{
// Creates file if it doens't exist
file_put_contents($name , $data);
}
exit();
}
?>
<html>
<body>
<form action = "?" method = "POST" id="raw-text" >
<textarea name="data" rows="10" id="raw" style="word-wrap: break-word; resize: horizontal; height: 700px; width: 99%"></textarea>
<input name="name" placeholder="File Name here"></input>
<button name="submit">Send!</button>
</form>
</body>
</html>
The above is capable of creating a file and inserting the data typed by the user.
If the php script is in a separate file, you can change the <form action = "?"
to <form action = "Path_To_File"
.
Note that:$_POST['…']
retrieves the named html tags from the form.
See http://php.net/manual/en/function.file-put-contents.php for details about the file_put_contents php function.
But, I agree with comments, this is not a secure way to do.
This code is an example and you need to test many things to make it secure and implement it in your application.
And you need to be sure that the files that the users will be creating aren't system files or php files that allow them to do anything they want.
PHP create text file on server
You have to make sure that:
- the folder /lib exists in the document root
- the
webserver process
has permission to write to that folder.
If you create the folder with your ftp account, the webserver process will have no access. You can set permissions to 777, but then everyone has access. Best would be to set permission to 770 and make the group of the folder the webserver group id.
How to create file in special directory using PHP
$path_to_file = " " //Make sure $path_to_file is the full path, something like /home/user/public_html/etc/ <br>
$yourFileName = 'myfile';
$ourFileHandle= fopen($path_to_file.$yourFileName, 'w') or die('Permission error');
Create a .txt file and store it on a specific folder Laravel
Check out the filesystems.php
file and add a specific configuration for the directory you want to read/write. After doing so, you will be able to create a file using the File facade by specifying your disk like so: File::disk('my-disk')
.
Related Topics
PHP Fatal Error: Call to Undefined Function MySQLi_Stmt_Get_Result()
How to Use PHP Reserved Names for My Functions and Classes
What Are the Differences Between Psr-0 and Psr-4
Regular Expression to Check If the String Has Chinese Chars
Php: Read Specific Line from File
Ini_Set("Memory_Limit") in PHP 5.3.3 Is Not Working at All
Str_Replace() on Multibyte Strings Dangerous
Is MySQL_Insert_Id Safe to Use
PHP Keep Checkbox Checked After Submitting Form
How to Export a SQL Table Without Access to the Server or PHPmyadmin
Which Mime Type Should I Use for Mp3
Insert PHP Code in Wordpress Page and Post
What Does This Mean? "Parse Error: Syntax Error, Unexpected T_Paamayim_Nekudotayim"
"Strict Standards: Only Variables Should Be Passed by Reference" Error