Showing posts with label PHP E_WARNING. Show all posts
Showing posts with label PHP E_WARNING. Show all posts

Saturday, 4 October 2014

is_writable in PHP

is_writable — Tells whether the filename is writable
Syntax:

bool is_writable ( string $filename )
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account.

Parameters:

filename
The filename being checked.

Return values: Returns TRUE if the filename exists and is writable.



Example #1 is_writable() example

<?php
$filename = 'test.txt';
if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}
?>
Errors/Exceptions ¶

Upon failure, an E_WARNING is emitted.



Note: The results of this function are cached. See clearstatcache() for more details.
Tip
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

is_writeable — Alias of is_writable
This function is an alias of: is_writable().

is_readable in PHP

is_readable — Tells whether a file exists and is readable
Syntax:

bool is_readable ( string $filename )
Tells whether a file exists and is readable.

Parameters:

filename
Path to the file.

Return values: Returns TRUE if the file or directory specified by filename exists and is readable, FALSE otherwise.



Example #1 is_readable() example

<?php
$filename = 'test.txt';
if (is_readable($filename)) {
    echo 'The file is readable';
} else {
    echo 'The file is not readable';
}
?>
Errors/Exceptions ¶

Upon failure, an E_WARNING is emitted.



Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account before PHP 5.1.5.

Note: The results of this function are cached. See clearstatcache() for more details.
Tip
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
Note:
The check is done using the real UID/GID instead of the effective one.
This function may return TRUE for directories. Use is_dir() to distinguish file and directory.

is_link in PHP

is_link — Tells whether the filename is a symbolic link
Syntax:

bool is_link ( string $filename )
Tells whether the given file is a symbolic link.

Parameters:

filename
Path to the file.

Return values: Returns TRUE if the filename exists and is a symbolic link, FALSE otherwise.



Example #1 Create and confirm if a file is a symbolic link

<?php
$link = 'uploads';

if (is_link($link)) {
    echo(readlink($link));
} else {
    symlink('uploads.php', $link);
}
?>
Errors/Exceptions ¶

Upon failure, an E_WARNING is emitted.



Note: The results of this function are cached. See clearstatcache() for more details.
Tip
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

is_executable in PHP

is_executable — Tells whether the filename is executable
Syntax:

bool is_executable ( string $filename )
Tells whether the filename is executable.

Parameters:

filename
Path to the file.

Return values: Returns TRUE if the filename exists and is executable, or FALSE on error.



Example #1 is_executable() example

<?php

$file = '/home/vincent/somefile.sh';

if (is_executable($file)) {
    echo $file.' is executable';
} else {
    echo $file.' is not executable';
}

?>
Errors/Exceptions ¶

Upon failure, an E_WARNING is emitted.



Note: The results of this function are cached. See clearstatcache() for more details.
Tip
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

unlink in PHP

unlink — Deletes a file
Syntax:

bool unlink ( string $filename [, resource $context ] )
Deletes filename. Similar to the Unix C unlink() function. A E_WARNING level error will be generated on failure.

Parameters:

filename
Path to the file.

context
Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams.
Return values: Returns TRUE on success or FALSE on failure.

Changelog ¶

Version Description
5.0.0 As of PHP 5.0.0 unlink() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support unlink().


Example #1 Basic unlink() usage

<?php
$fh = fopen('test.html', 'a');
fwrite($fh, '<h1>Hello world!</h1>');
fclose($fh);

unlink('test.html');
?>

Thursday, 4 September 2014

PHP: What is the best way to remove or turn off warning messages in PHP?

There will be times when you will see a warning message output to the browser after running your PHP script and you may want to turn off that warning message. Obviously, it’s a lot better to get to the root of the problem and fix that instead. But, if you know that you do not need to fix the root of the problem (for whatever reason), in order to remove a warning message in PHP all you have to do is use the error_reporting function in PHP. If you don’t care about how the function works, then just skip to the section that says “The code to turn off error messages in PHP” to see the code that you should use to turn off warning messages in PHP. Otherwise, keep reading.

Using the error_reporting function to turn off warnings in PHP

The error_reporting function in PHP basically allows you to set the kind of error reporting that you want. How does the error_reporting function work? Well, you simply pass in the type of errors to the error_reporting function that you want to have reported on the page – you need to pass in constants (which are text fields that translate to numbers) to the error_reporting function .
The E_PARSE constant tells PHP that compile time parse errors should be reported and displayed on the page as you can read about here. Since you definitely want to know about any compile time errors, you should pass this constant to the function. The E_ERROR constant tells PHP that the details of any fatal run-time errors should be reported and displayed – this is also something you definitely want, since you should always know what the cause of any fatal run-time errors is.
Now that you understand a bit more about how the error_reporting function works – here is the actual code to use:

The code to turn off error messages in PHP:

You have to place this line of code before the code that is causing the warning to be displayed. If you place this code after the offending code, then it will not work in suppressing the error message that gets displayed. Here is the line of code to use:
error_reporting(E_ERROR | E_PARSE);

The code above does not have the E_WARNING constant being passed in

Because the function above does not include the “E_WARNING” constant, the non-fatal run-time warnings will not be displayed on the page when a PHP script is run. And that is exactly what prevents the warning message from appearing on the page.

How do the constants work in the error_reporting function?

In the example above, E_PARSE and E_ERROR are both constants – which means that they are actually numbers represented by text, so E_PARSE really is just some text that represents the numeric value of 4, and E_ERROR is text representing the numeric value of 1. Read on to understand how those constants work.

How does the OR operator work with error_reporting function?

Note that the function above uses the “|” – the OR logical operator, which is applied against the constants that are passed into the error_reporting function. You will notice if you look at this page that those constants are all multiples of 2 – the reason for this is because when they are “OR’ed, the appropriate bits will be retained and that will tell the error_reporting function what errors need to be displayed.

Another way to hide or remove warning messages in PHP

Another option to remove warning messages in PHP is to use what is called the error control operator – which is basically just the at sign – the “@”. When the “@” sign is put in front of an expression, any error message that might be generated by that expression will be ignored.
The “@” error control prefix operator will even disable error reporting for critical run time errors. For this reason, you should only use this operator if you really know what you are doing. The “@” can only be used in front of expressions – so it can not be used in front of a function or class definition, a for loop, etc. But, it can be used in front of a call to a function. Here is what it would look like in that scenario:
@someFunctionCall( );