Step 1:
Create a php script file and codes will remain inside php tag.
Step 2:
Specify your text and save it to a variable.
Suppose, you want to save the text: "Let's make a text file using php." Then, store it to a variable.
Step 3:
Create a blank text file with your expected name and extension. For this example, we will create a example.txt file. Syntax of this code is fopen("file/pathname","permission"). If you want to store the text file into your current directory from which the script is running then just give the name of the file. But, if you want to store it to a specific location, then you have to give complete path with the name of the creating file. Suppose, your script is located at "root/scripts/file/", then with only the name of file will bd created with location: "root/scripts/file/example.txt", but if you want to store it to root directory then give path like "../../example.txt". Permissions are usually write/read. If you want to just create the file as read only mode, keep permission "r". Or, if you want to keep it in write mode, then keep "w" or "r+". We will keep it "w" (write permission mode) for writing a text into it. And we are going to store the file into the same directory of the script's location.
Step 4:
Store the text into the file. Require php syntax for this action is fwrite("file variable", "Text/ text variable"). As we have created the file and stored it's information in $file variable and already stored the text into the variable into $text variable, our syntax will be like fwrite($file,$text);
Step 5:
Close the file and end php tax.
This is a simple tutorial of creating a text file. You will need to do extended coding for fulfilling additional requirements of your project. It will be great pleasure of mine, if this tutorial helps you.
No comments:
Post a Comment