Learn PHP
Site Map
PHP
HTML
Authentication
Php Functions
C++ - Starting Out
Web Design Tools
Photoshop Tutorials
Windows
Phones
Droid Phones
Building Websites
ArrowTech Blog
Web Servers
Networking
Making Money
Favorite Links
About
ObamaCare
CSS
Software
Funny
Privacy Policy
JavaScript
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 make browser go back to last page with using php

How To Make Browser Go Back To Last Page With Php8/12/2014After submitting an HTML form or clicking a link encoded with variables and information, it is often desirable to the programmer that the user's browser be automatically redirected - to go back to the previous p

binary search user functions

Binary Search User FunctionThe code in the example is for finding a given value in a sorted array.If the value is not found the function will return the position of the location where it the value should be.

php conditional statments if else switch

Conditional Statements If Else & Switch CaseIf ElseIf else is the bread an butter of programming - so-to-speak. if-else and Case Switch are known as conditional operators; they create conditions or criteria that a block of

textarea code overflow

Textarea Code Overflows and Runs Out Onto PageMy Decision to Use Textarea TagWhile working on a script for to edit content, I ran into few problems. First - because mrarrowhead is a website about building websites using php, I have had to battle with the best way to disp

creating image watermarks

Creating PHP Watermarks$my_image = imagecreatefromjpeg('photo.jpeg');$watermark = imagecreatetruecolor(100, 70);imagefilledrectangle($watermark, 0,0, 99, 99, 0x0000FF);imagefilledrectangle($watermark, 9

odd number php script

Testing for Odd/Even Numbers - PHPExample - Banner Rotation PHP Script Odd/Even NumbersSimple Php Tutorial Creating Odd / Even Testing% modulus operator.$_SESSION['x'] % 2;

using php cookies variables

Using Cookie Variables in PHPSetting cookies in php is pretty straight forward. There is a php functionsetcookie();This function must be called before any anything else on the page or it will fail. There are two options to address

how to unzip files on your free hosting account

Uzipping Files on Your Web ServerIf 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 of features. One feature that I could not find on Godaddy.com free hosting accounts was a utility tha

passing data from one form to another form using php

Passing data from one form to another formTo pass php data from a form and then catch that data into another form one way to accomplish this task is to intermingle your php code into your HTML to dynamically capture variable values. This particular method is

Just In Time Compiling

JIT - Just in Time Compilers JIT refers to a special software that is found in web-browsers, and scripting engines such PHP's Zend. (My understanding of ZEND is that it produces byte-code and functions similar to other JIT compilers). For this discussion, without tryin

Storing image locations mysql

Store Image URL in DatabaseRetrieve Image with PHP Script Saving URL Text in Database Saving image filesystem paths, of an image file in

how to turn variable value into variable name

Variable Variables in PHPEvery so often when you are writing a php file, we all run into a situation where we would like for the name of the string that is inside of your php variable to be converted into the actual name of another variable.

what-is-php

Explanation of PHP Basic Syntax --New to PHPPhp is a language that is very much like writing c. The best feature of PHP versus c is that it is designed primarily to be used on WebPages -- allowing the author to intermingle their php code and the HTML on t

how to use dreamweaver

Dreamweaver - WISIWIGDreamweaver is a tool that arguably could be called the industry standard for web design.Dreamweaver, is an Adobe product that originally belonged to Macromedia. They sold out to Adobe a few years ago. I have been using Adobe/Macromedia Dreamweaver for

Filezilla

Filezilla Ftp Client, FTP SERVER vs WsftpBy D.Shaun MorganWhile I was writing this article, about halfway through, I realized that it would be unfair to tell my readers about Filezilla without also mentioning another very good FTP client called WSftp. So, I backtracked and

notepad plus

Notepad++Download Notepad Plus Plus Notepad plus plus is an open source, free text editor. It is an ideal choice for many programming and scripting languages. Example - How to write c++ w

how to open create write files using php

Php file functions, Opening a file in php, reading a file in php, writing a file in phpIn 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 nonethel

stop form spam captcha

PHP -- How to Protect My Email Form / Contact Form Against Spam BotsAuthor D.Shaun MorganRandom Name Fields to Stop SpambotRandom Name Fi

retreive images mysql using php

How to get images out of mysql database with php and use them on my webpagesAuthor D.Shaun MorganPHP Version - PHP 5xMysql Version - 5Tutorial OutlineC

store images mysql using php

Storing Images in Mysql DatabaseTutorial OutlineCreating a Test Mysql DatabaseExample php Database FunctionExplanation of Database Function

php example post array

Visual example of how PHP handles the super-global array variable "$_POST"Author D.Shaun MorganVersions and Skill LevelPHP Version - PHP 5xReader skill level - BeginnerXHTML 1.0 Transition

php arrays

Passing PHP ArraysHow to pass php variable arrays between pages.Author Author D.Shaun Morgan

how to write php functions

Explanation of a Basic PHP FunctionWhat is a PHP function?In PHP a function represents a block of instructions that perform a task. A PHP Function allows the user to re-use blocks of script without having to rewrite all the

how to write html form using php

When learning PHP to build a website, one of the very first things that any newcomer, or beginner needs to learn is how to use HTML or XHTML ( will mean the same for this tutorial.) forms to collect information from users. Those data are usually email addresses, names, birthdays, and

register globals long arrays

Security, php.ini, register_globals and register_long_arraysAuthor D.Shaun MorganVersions and Skill LevelPHP Version - PHP 5xReader skill level - ALLTutorial Outline

how to install php 5

How to Install PHP 5.x.x.x on WindowsThis is beginner's how-to manual explaining how to quickly setup a PHP 5 installation on a Windows PC. You must have an Apache webserver installed and working on your personal computer.This tutorial was written primarily for configuri

php passing variables

Pass Variables from One Page to Another -- PHPTutorial Outline Passing PHP variables with

.

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.