Php-mysql Course

PHP has multiple functions to handle files. First, it's important to know that if the PHP server runs on a Linux server, to create or alter files, you normally need to set global access permissions of 0777.
If you use PHP server (WampServer, XAMPP) on Windows you don't need to set these CHMOD permissions.

Creating new files

The simplest way to create a new file is to use the file_put_contents().
  - Syntax:
file_put_contents('filename', 'string', flag)
If "filename" doesn't exist, the file is created with the "string" content. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
The flag parameter is optional. If is set with FILE_APPEND, if file "filename" already exists, append the data to the file instead of overwriting it.
- This function returns the number of bytes that were written to the file, or FALSE on failure.

Try the next example. Create a new folder (named "files") in the 'www' (or 'htdocs') server directory.
Create a PHP file in the "www" server folder, and add the fallowing code to it:
<?php
$filename = 'files/test.txt';
$str = 'Free PHP course and tutorials.'. PHP_EOL. 'Web site: https://coursesweb.net';

// creates the 'text.txt' file in the 'files' folder
if (file_put_contents($filename, $str)) echo 'The file was created';
else echo 'The test.txt file can not be created';
?>
- PHP_EOL is a PHP constant that represents a new line on any operating system.
If you run this script in your browser, it should create a "test.txt" file in the "files" directory, with the content added in the $str variable, and displays: "The file was created" (This file will also be used in the next examples).

Using file_get_contents and readfile

The simplest way to read the contents of a text file is to use one of the fallowing functions: With these function, PHP can read files in a single operation.

  - Example (with file_get_contents):
<?php
$filename = 'files/test.txt';

// check if the file exists
if (file_exists($filename)) {
  $get_data = file_get_contents($filename);        // store the file content in $get_data
  echo $get_data;                              // outputs the content
}
?>
  - Example (with readfile):
<?php
$filename = 'files/test.txt';

// check if the file exists
if (file_exists($filename)) {
  readfile($filename);           // gets and outputs the content
}
?>
These examples use the "text.txt" file created in the previous example.
It's indicated to check if the file exists before to read it, this thing avoid the errors. The "file_exists('filename')" returns TRUE if the "filename" exists, otherwise False.
With the file_get_contents() function you can also get the content of a file located on other server (i.e. file_get_contents('http://site_name/page') ).

Using the file() function

With the file() function, the PHP script can read and store text file content into an array. It returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure FALSE.
  - Syntax:
file('filename', flag)
The argument for "flag" is optional. This parameter can have the fallowing values:
  - Example:
<?php
$filename = 'files/test.txt';

// check if the file exists
if (file_exists($filename)) {
  $ar_rows = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);           // gets the lines in an array

  var_export($ar_rows);              // outputs the structure of the $ar_rows array
}
?>
Assuming that the "test.txt" is the file created in the first example, this code will output:
array ( 0 => 'Free PHP course and tutorials. ', 1 => ' Web site: https://coursesweb.net', )

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Using file_put_contents, file_get_contents, readfile and file()

Last accessed pages

  1. Introduction to Adobe Flash (2486)
  2. PHP getElementById and getElementsByTagName (49158)
  3. Methods of the String object in JS (196)
  4. Code Snippets - Add and Create (1800)
  5. Send Email with Nodemailer (3166)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (324)
  2. Read Excel file data in PHP - PhpExcelReader (118)
  3. The Four Agreements (97)
  4. PHP Unzipper - Extract Zip, Rar Archives (94)
  5. The Mastery of Love (87)