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 1)

By Exforsys | on November 15, 2006 |
PHP Tutorial

PHP Tutorials – File manipulation (Part 1)

In the PHP Tutorial You will learn about File manipulation (Part 1) – Checking file existence, A file or directory, Determining file size, Creating and deleting files and Opening a file for writing, reading, or appending.

Checking file existence:

You can test for the existence of a file with the file_exists() function.
file_exists() takes one element, which is a string representing an absolute or relative path to a file that might or might not be there.
If the file is found, file_exists() returns true; otherwise, it returns false.

Example:

if (file_exists(“file.jpg”)) {
print “File exists”;
}

A file or directory:

You can confirm that the entity you’re testing is a file, as opposed to a directory, with the is_file() function.
is_file() takes one argument, which is the file path and returns a Boolean value.

Example:

if (is_file(“file.jpg”)) {
print “file.jpg is a file”;
}

To check that the entity you’re testing is a directory, you can do this with the is_dir(). is_dir() takes one argument, which is the directory path and returns a Boolean value.

Example:

if (is_dir(“/dir”)) {
print “/dir is a directory”;
}

Determining file size:

filesize() attempts to determine and return its size in bytes.
filesize() takes one argument, which is the file path. It returns false if it encounters problems.

Example:

print “The size of file.jpg is.. “;
print filesize(“file.jpg”);

Creating and deleting files:

If a file does not yet exist, you can create it with the touch() function.
touch() takes one argument, which is a string representing a file path
touch() attempts to create an empty file of that name.
If the file already exists, the contents aren’t disturbed, but the modification date is updated to the time at which the function executed.

touch(“file.txt”);

You can remove an existing file with the unlink() function.
unlink() accepts a file path:

unlink(“file.txt”);

All functions that create, delete, read, write, and modify files on Unix systems require the correct file or directory permissions to be set.

Opening a file for writing, reading, or appending:

Before you can work with a file, you must first open it for reading, writing, or both, to do so use the fopen() function.
fopen() takes two argument, a string that contains the file path and a string containing the mode in which the file is to be opened.
The most common modes are read (r), write (w), and append (a). fopen() returns a file resource.

To open a file for reading, you use the following:

$fp = fopen(“file.text”, ‘r’);

You use the following to open a file for writing:

$fp = fopen(“file.text”, ‘w’);

To open a file for appending (that is, to add data to the end of a file), you use this:

$fp = fopen(“file.text”, ‘a’);

fopen() returns false if the file cannot be opened for any reason.

It’s a good idea to test the function’s return value before proceeding to work with it. You can do so with an if statement:

if ($fp = fopen(“file.txt”, ‘w’)) {
// Execute this code
}

Or you can use a logical operator to end execution if an essential file can’t be opened:

($fp = fopen(“file.txt”, ‘w’)) or die (“Couldn’t open file”);

If the fopen() function returns true, the rest of the expression won’t be parsed, and the die() function (which writes a message to the browser and ends the script) is never reached. Otherwise, the right side of the or operator is parsed and the die() function is called.

Assuming that all is well and you go on to work with your open file, you should remember to close it when you finish. You can do so by calling fclose()

fclose() takes one argument, which is the file resource returned from a successful fopen() call as its argument:

fclose($fp);

« « PHP Tutorials – Arrays (Part 2)
Duties of a Banker » »

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 – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Strings (Part I)

    October 13, 2006 - 0 Comment
  • WAMP Server

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

    November 22, 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 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-2)

    November 22, 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