Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP Tutorials – File manipulation (Part-2)

By Exforsys | on November 22, 2006 |
PHP Tutorial

File manipulation (Part-2)

In this PHP Tutorial you will learn the 2nd Part of File Manipulation Reading lines from a file, Reading arbitrary amounts of data from a file, Writing to a file, Creating directories, Removing a directory and Opening a directory for reading.

Reading lines from a file:

To read a line from an open file, you can use fgets().

fgets() requires the file resource returned from fopen() as an argument.

You may also pass fgets() an integer as a second argument.

The integer argument specifies the number of bytes that the function should read if it doesn’t first encounter a line end or the end of the file.

fgets() reads the file until it reaches a newline character ("\n"), the number of bytes specified in the length argument, or the end of the file.

$line = fgets($fp, 1024);

Although you can read lines with fgets(), you need some way to tell when you reach the end of the file.
The feof() function does this by returning true when the end of the file has been reached and false otherwise. feof() requires a file resource as its argument.

feof($fp);

Example:

<?php
$filename = “file.txt”;
fp = fopen($filename, ‘r’) or die(“Can’t open”);
while (!feof($fp)) {
$line = fgets($fp, 1024);
print “$line<br>”;
}
?>

Reading arbitrary amounts of data from a file:

Rather than reading text by the line, you can choose to read a file in arbitrarily defined chunks.

The fread() function accepts a file resource as an argument, as well as the number of bytes you want to read.

fread() returns the amount of data you requested, unless the end of the file is reached first.

Example:

<?php
$filename = “file.txt”;
$fp = fopen($filename, ‘r’) or die(“Can’t open”);
while (!feof($fp)) {
$chunk = fread($fp, 16);
print “$chunk<br>”;
}
?>

Writing to a file:

fwrite() accepts a file resource and a string, and then writes the string to the file. fputs() works in exactly the same way.

Example:

<?php
$filename = “file.txt”;
$fp = fopen($filename, ‘w’) or die(“Can’t open”);
fwrite($fp, “Written by:\n”);
fclose($fp);
$fp = fopen($filename, ‘a’) or die(“Can’t open”);
fputs($fp, “PHP\n”);
fclose($fp);
?>

Creating directories:

mkdir() enables you to create a directory.

mkdir() requires a string that represents the path to the directory you want to create, and an octal number integer that represents the mode you want to set for the directory.

You specify an octal (base 8) number with a leading 0. The mode argument has an effect only on Unix systems.

The mode should consist of three numbers between 0 and 7, representing permissions for the directory owner, group, and everyone, respectively.

mkdir() returns true if it successfully creates a directory, or false if it doesn’t. If mkdir() fails, it’s usually because the containing directory has permissions that preclude processes with the script’s user ID from writing.

mkdir(“dir”, 0777);
mkdir(“dir”, 0755);

Removing a directory:

rmdir() enables you to remove a directory from the file system if the directory is empty.
rmdir() requires only a string representing the path to the directory you want to create.

rmdir(“dir”);

Opening a directory for reading:

Before you can read the contents of a directory, you must first obtain a directory resource. You can do so with the opendir() function.

opendir() requires a string that represents the path to the directory you want to open. opendir() returns a directory handle unless the directory isn’t present or readable; in that case, it returns false.

$handle = opendir(“testdir”);

« « Sample Resume – Computer Operator Resume
PHP Tutorials – Forms » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • PHP Tutorials – Strings (Part I)

    October 13, 2006 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Strings (Part 2)

    October 20, 2006 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP Tutorials – Variables

    September 22, 2006 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • PHP Strings
  • How to Use Cookies in PHP
  • PHP and MySQL User Registration
  • WAMP Server
  • PHP Tutorial : PHP & MySQL

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT