Category: php

.


Php file functions, Opening a file in php, reading a file in php, writing a file in php


In this tutorial we will look at the most commonly used file functions used in php. The examples below should be pretty familiar to the seasoned php scripter, but they are useful nonetheless. We will end this tutorial with a php script and an html form that will read the contents of a file into a string variable and output it into a textfield for editing. You can try the script out below.

Php offers programmers and web design experts serveral useful and built in functions for handling files.
Here I show you how to get content from a file, read it into a php variable, and then write it into another file.  The example below is

Here is our first example.

 

<?php
$file = 'mytestfile_01.txt'; //the name of our file
$handle = fopen($file, 'a'); //now we open the file and give it a handle to refer to it
$contents = fread($handle, filesize($filename)); //read the contents into the file
fclose($handle);
        $file = 'mytestfile_02.txt'; //the name of our second test file
        $handle = fopen($file, 'wb');
        fwrite($handle, $contents);      //write mytestfile_01.txt content into mytestfile_02.txt.
        fclose($handle); //close the file
?>

The above example works pretty good and will do the job, but fortunatley if you just want to read the contents of a file and store it into another file on your linux or windows system there is two php functions that will make writing your script much easier.

string file_get_contents ( string $filename [, int $flags = 0 [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]])

string file_put_contents ( string $filename [, int $flags = 0 [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]] )

These two PHP functions give us the same result as all that code in the first example. Lets look at how to use these PHP file handling functions.

<?php

 

$file = 'mytestfile_01.txt';
$content = file_get_contents($file);
file_put_contents($file, $content);

?>

Finally lets take these two examples and  put together a php script that will allow you to open a file on your server, edit it, and then save it. The following is a simple tool that I use with many of my websites. While this is not the best text editor in the world, it really comes in handy when you are away from your desk and you see a place on your site that you want to edit. You just open up the content, edit it and then save it back right hrere on your server. Also, if you are hosting your website somewhere like Godaddy.com, you will definitely want to set up yourself with a qick way to edit your files on the go, because logging onto Godaddy can be time consuming, even on a fast connection.

First make a folder and name it /arrowhead_editor/. Then make a file and called /the_editor.php the_editor.php and place it inside.

<?php
$file = 'test.php';
if(!empty($_GET['content'])){

    $content =  $_GET['content'];
    file_put_contents($file,$content); //NOTE these files will be made if they don't exist already
    $content = file_get_contents($file);
}else{
    $_GET['content'] = NULL;
    $content = file_get_contents($file);   
}
?>
<html>
<head>

<link rel=stylesheet type=text/css href=editor.css /> bk

<title> Mr Arrowhead Editor </title>
</head>

<body>

<b> Mr Arrowhead Editor </b>
<form method=get action=the_editor.php>

<input type=submit />
<br/><br/>
<textarea cols=100 rows=75 name=content>
<?php echo $content; ?>
</textarea>
<br/><br/>
<input type=submit name= />

</form>


</body>

Author: D.Shaun Morgan

how to unzip files on your free hosting account Uzipping Files on Your Web Server If you are using a free hosting account with godaddy.com and many of the other Free hosts out there, you may have found that they don't offer you a lot o

passing data from one form to another form php Passing data from one form to another form To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi

how to open create write files php Php file functions, Opening a file in php, reading a file in php, writing a file in php In this tutorial we will look at the most commonly used file functions used in php. The

stop form spam captcha php PHP -- How to Protect My Email Form / Contact Form Against Spam Bots Author: D.Shaun Morgan Versions and Skill Level PHP 5

register globals long arrays php Security, php.ini, register_globals and register_long_arrays Author: D.Shaun Morgan Versions and Skill Level PHP Version - PHP 5x

.
 

Leave a Comment

how-to-open-create-write-files-php

Name:      | Email:      | Website:

Comment:


captcha

Enter The Above Security Code:


Author: James | Website URL: . |
Comment:
Great Tutorial I love this write up, best one I have read so far about creating files using php.