How Do I Create And Write To Files? - creating, writing, deleting files

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

How Do I Create And Write To Files? - creating, writing, deleting files

alfonzo
Hi,

Can someone please tell me how to create files and write to them in PHP. I just want to create a simple file containing text, and then be able to read it or update it.

Thanks
Alfie

Reply

sirknight
Before you can write data into a file this should be existed:
Here is a sample code on how to read/write into a file.

Create a new php file then type this:
CODE

<?php
$myFile = "test.txt";
$filehandler = fopen($myFile, 'w') or die("can't open file");
$stringData = "You're text here \r";
fwrite($filehandler, $stringData);
$stringData = "Another Text here \r";
fwrite($filehandler, $stringData);
fclose($filehandler );
?>


Explanation:

Var $myFile on the first line declares the filename of you're file.If its on different directory please specify that directory where it is located.

The second line creates a variablle that will handle the file..That is why i called this file handler. fopen() is a function that will open that file. 'w' is for you to write..If you only want to read it just make it 'r'

$stringData variable is the string or character you want to write on the file plus a carriage return.

The fourth line will actually do the writing. fwrite() function.It takes two parameter the filehandler and the string you will write which is $stringData.

And everytime you open a file you will also need to close this using fclose() function.

Hope it helps.

 

 

 


Reply

alfonzo
Thats great thanks smile.gif

Reply

Umar Shah

CODE
<?php
$myFile = "test.txt";
$filehandler = fopen($myFile, 'w') or die("can't open file");
// this will create a new file if the file does not exist other wise overwrite it
// to ensure that you dont overwrite an existing file but append to it use fopen($myFile, 'a')
// if you want to read and write to the same file use w+/a+ mode to open the file

$stringData = "You're text here \r";
fwrite($filehandler, $stringData);
$stringData = "Another Text here \r";
fwrite($filehandler, $stringData);
fclose($filehandler );
?>


Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : create, write, files, creating, writing, deleting, files

  1. Magic Quotes And $_files
    (3)
  2. How To Protect Included Files
    (10)
    Hey! How can i make my included files 100% safe. Like if I include a file witht his code..
    CODE include "bla.php"; How can i make it 100% safe? I know I must close the php tags
    in the included files. but what more =?....
  3. Writing And Testing My Own Login Script [solved]
    (20)
    i have this error QUOTE Warning: session_start() : Cannot send session cache limiter - headers
    already sent (output started at /home/eggie/public_html/race.php:2) in
    /home/eggie/public_html/race.php on line 5 in every page i have with session start...what's
    the problem?? CODE <title>Race</title> <?php
    include("style.css"); include("config.php");
    session_start(); if(!session_is_registered(myusername)){ echo 'Your
    Session has Expired!'; exit;} //If you click race.....
  4. Reading Files And Folders
    (1)
    I am in the process of writing a small content management system for my niche sites and need to be
    able to accomplish the following two items. Any assistance would be appreciated: I have a
    directory that has nothing but sub directories in it and I need to be able to identify each
    directory name and return them in an option drop down selection menu which will be included in an
    identifying URL. ALSO: I am in need of a script that could be included on a returned page
    that will read the content of a table and identify various keywords within the content and pl....
  5. Php File Upload
    About uploading files through php (3)
    Right i have done a check for a tutorial on this as well as a question about it but php is not
    allowed in the search box. So i thought i'd just ask what i want to know. I have a form which
    uploads a file, it refreshes the page, uploads the file and then alerts the user to if the file has
    uploaded. To be honest im not sure why i keep getting the error. But here is the code: This is the
    form that is used for the user to select the file &fid= " method="POST"> Choose a file to
    upload: This is the upload code if ($op ==....
  6. Help To Transfer Files Within Hosting Space Using Php
    (4)
    I want to transfer files from one folder to another or an entire folder in my web space. I don't
    want to download it and upload it, can I do this using a PHP script. Please help me. I'm just
    learning the basics of PHP....
  7. Login Script
    PHP Help #3 - Need help creating one (5)
    It turns out that the authentication script that I copied from
    http://www.php-mysql-tutorial.com/user-aut...on/database.php doesn't work even when it is left
    unchanged. What a crappy piece of code. Now I am trying to build by own login script from scratch.
    I already have a little knowledge on how to do this (connecting, echoing, retrieving) but I need
    some more examples and/or tips. I know what I need and maybe this could help you out: Note: Green
    items are fixed. No duplicate username in MySQL Database Authorized users only. I have to
    authorize each....
  8. How To Edit Php Files?
    (16)
    Hi, You can use frontpage to preview HTML files, but how can I edit and preview PHP files offline.
    I'm not well-known with PHP and I see more and more using this script to manage their site.....
  9. Xgrid With Php
    Creating a script to post a blender file to Xgrid using PHP (0)
    I am doing pre planning for the blenderxgrid.com script. I was originally going to do this with
    PERL, but elected against it. Eventually I'll be moving the site off astahost and replacing it
    with a website hosted on the same machine as the xgrid controller. I am setting up a test version
    on my latop using OSX's apache server, MySQL, and PHP on a localhost config. Here is my step
    list for the script: ------------------------------------- Form: (within Xoops CMS, so user will
    have to be logged in) Username Password (where they can upload the .blend file....
  10. How To Delete Files When Session Ends
    (4)
    Dear Friends I need solution to a problem. The problem is as under: I am creating certain files
    (playlist) in server disk when user selects some songs. The files are created in ram format. What I
    want to do is to delete these files created during a particular session. Is it possible to do so?
    Now I am deleting these files using on Unload event fired by JavaScript. I am using PHP. ....
  11. Help: Php + GD - Creating A Progress Bar
    Creating A Progress Bar (5)
    I've recently been trying to read up on using PHP and GD to create PNG's on the fly.
    I've been doing so in hopes that I will be able to learn how to compile a script that will
    output an image of a progress bar with certain attributes based on the url that called it (which
    will be accessed predominately from a forum, so it must be able to be coded as standard BBC). These
    attributes would include the bar's title (based on the event mentioned in the url) printed
    centered and right above the meter, a background image (to hold the meter), the meter (the ....
  12. Deleting Files With PHP
    is this possible? (6)
    It is posible for PHP to delete files on a server? If so, how is this possible? Just out of
    interest.......
  13. Permission Denied In Creating A Directory
    mkdir() (6)
    QUOTE Warning: mkdir(/home/whistle/public_html/see/seeto): Permission denied in
    /home/whistle/public_html/see/config.php on line 56 My web hosting is on the astahost. The
    absolute path of my php script is "(/home/whistle/public_html/see". When I try to create a directory
    named "seeto", the error message about permission denied is displayed. The same script I tried on
    another web hosting server "Lycos", it works perfectly. Can anyone tell me what is wrong with it?
    Or anything I should notice more? Thanks. CODE <?php if (mkdir("/home/whistle/p....
  14. Php : Variables Included Dont Work In Functions
    Variables from Included files dont work (4)
    Today, I came up with this strange PHP behaviour. Just wanted to know if anyone has any
    suggestions! I make a common variable/function file called config.php. I put in my generally
    used functions in it. Suppose this is my file // -----VARIABLES --- // $a=10,$b....
    // -----FUCTIONS--- // function doit() { print "A value is " . $a; } ?> Here, suppose we
    execute this file directly. Since A has a global scope, it does work perfectly. But if this same
    file is imported in another file say, mainfile.php // -----VARIABLES --- // $c,$....
  15. Php Reading And Writing To File
    the code is very esay (4)
    this code to Read from file you can use this code in to make a small data base and it is very to
    use CODE $fp = fopen ("file.txt", "r"); $bytes = 4;
    $buffer = fread($fp, $bytes); fclose ($fp); print
    $buffer; ------------------- to write to same file CODE $fp = fopen
    ("file.txt", "w+"); fwrite ($fp, "Test"); fclose
    ($fp); ---------------- thanks and iwait the commant....
  16. Getting Files From Other Sites
    Using wget (3)
    Hello. A while ago, a friend showed me this PHP code (wget) which allows you to get files from other
    sites. I was a simple one liner type thing. Since my old host (Nytka) went down, I've lost it,
    as has my friend. Do you know anything about wget, and could tell me it?....
  17. Free software for creating PHP sites
    (17)
    This is good for those who doesnt have knowledge bout PHP they are totally free here are my 2 know
    free web portal system Postnuke -> www.postnuke.com and PHPnuke -> www.phpnuke.org /tongue.gif'
    border='0' style='vertical-align:middle' alt='tongue.gif' /> ....

    1. Looking for create, write, files, creating, writing, deleting, files

Searching Video's for create, write, files, creating, writing, deleting, files
advertisement




How Do I Create And Write To Files? - creating, writing, deleting files



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE