How can I avoid the "No such file or directory" warning
I recently worked on a project where I needed to rename a picture. The problem is when I rename the image it renamed, but I show a warning like below:
Warning: Rename (looking for / SeekerPhoto / katr.jpg looking for /SeekerPhoto/ussl.jpg) [function.rename]: No such file or directory in /subdomains/www/html/ussl/job/insphoto.php on line 100
How can I avoid this warning. This means that there was a warning, this will go for the next task.
You can do some testing on the parameter before renaming the file.
if ( !file_exists($oldfile) || !is_readable($oldfile) ) {
// some error handling here
}
else {
$b = rename($oldfile, $newfile);
}
edit: I didn't expect this answer to be much higher than the others. Please take a look at the comments. It is very difficult / almost impossible to test all conditions that might trigger a warning here beforehand. Do what has ever been tested on the filesystem when you take the actual action that might fail. Field of test tables, permissions, or whatever you need and your SQL query may fail (for example, a server with server 2006-MySQL is gone can happen at any time). And so on and on. However, you can check the parameters for more likely reasons and let the script handle these errors gracefully.
a source to share
You can use the @ operator , which suppresses error messages for individual operators.
@rename($oldFileName, $newFileName);
Alternatively, you can decrease the value error_reporting
if you want to suppress warnings for multiple statements:
$oldErrorReportingValue = error_reporting(0);
rename($oldFileName, $newFileName);
# do something else ....
error_reporting($oldErrorReportingValue);
Please note that warnings exist for a reason. A better approach would be to figure out why the operation generates a warning and takes care so that your code can handle those situations. You should only ignore warnings as a last resort.
a source to share
Two things that are used together should best serve you:
Use error_reporting()
to set the appropriate level of detail for warning messages. note that only sets which warnings, notifications, and / or errors are logged, not whether they are displayed.
In your case, perhaps " error_reporting( E_ERROR | E_USER_ERROR )
;" which will only log anything if it's actually a bug, not just a notification or warning, which doesn't actually break anything.
Overall, it's probably a good idea to do something like this:
if (getenv('PHP_DEBUG')=='1')
{
error_reporting( E_ERROR | E_USER_ERROR );
ini_set( 'display_errors', true );
}
else
{
error_reporting( E_ERROR | E_USER_ERROR );
ini_set( 'display_errors', false );
}
And then on the development server you might have the following line in your .htaccess or VirtualHost directive:
SetEnv PHP_DEBUG=1
It is not necessary to install it at all in production, since it is not installed ≠ 1.
On the side of the note, I personally prefer that my error_reporting set:
error_reporting( E_ALL | E_STRICT );
which you could read in English as "warn about everything I could have done wrong to make me work better" because I feel like if I can beat every notification and warning just by checking some things before using them, and initializing the variables correctly, the end result is likely to be at least slightly safer.
edit : some clarification:
Since Arif did not ask to be sure that the operation succeeded, just to avoid receiving the message. Which I interpreted as "don't care if the operation works". Of course, the best way to do this in your function library would be:
/**
* @author: Kris
* @license: see http://sam.zoy.org/wtfpl/
*
* PLEASE NOTE THAT THE FOLLOWING CODE IS TESTED BY ME, NOT QUALITY ASSURANCE
*/
/**
* Move a file
*
* If uses filename from $source if $destination is a directory
*
* @param string $source
* @param string $destination
* @param bool $overwrite
* @return bool
*/
function my_move_file( $source, $destination, $overwrite = false )
{
return _internal_my_move_or_copy_file( $source, $destination, true, $overwrite );
}
/**
* Copy a file
*
* If uses filename from $source if $destination is a directory
*
* @param string $source
* @param string $destination
* @param bool $overwrite
* @return bool
*/
function my_copy_file( $source, $destination, $overwrite = false )
{
return _internal_my_move_or_copy_file( $source, $destination, false, $overwrite );
}
define( '__internal_my_move_or_copy_file_e_error', E_USER_ERROR ); // change to E_USER_NOTICE if not meant to be fatal
define( '__internal_my_move_or_copy_file_e_notice', E_USER_NOTICE );
/**
* Should not be called by userland code, use my_move_file or my_copy_file instead
*
* one function to implement both move and copy because almost all of the required validations is identical.
*
* @param string $source
* @param string $destination
* @param bool $is_move
* @param bool $overwrite
* @return bool
*/
function _internal_my_move_or_copy_file( $source, $destination, $is_move, $overwrite )
{
// what we'll be returning
$result = false;
// input sanity checks
if ( !is_string( $source ) || !is_callable( $source, '__toString' ) )
{
trigger_error(
"_internal_my_move_or_copy_file: expects \$source to be a string.",
__internal_my_move_or_copy_file_e_error );
return false;
}
elseif ( !is_string( $destination ) || !is_callable( $destination, '__toString' ) )
{
trigger_error(
"_internal_my_move_or_copy_file: expects \$destination to be a string.",
__internal_my_move_or_copy_file_e_error );
return false;
}
elseif ( ! is_bool( $is_move ) )
{
trigger_error(
"_internal_my_move_or_copy_file: expects \$is_move to be a bool.",
__internal_my_move_or_copy_file_e_error );
return false;
}
elseif ( ! is_bool( $overwrite ) )
{
trigger_error(
"_internal_my_move_or_copy_file: expects \$overwrite to be a bool.",
__internal_my_move_or_copy_file_e_error );
return false;
}
$action_word = $is_move ? 'move' : 'copy';
if ( file_exists( $source ) && is_readable( $source ) )
{
$to = preg_split( '/\//', $destination, -1, PREG_SPLIT_NO_EMPTY );
$destination = '/'.implode( '/', $to );
if ( is_dir( $destination ) )
{
// make sure we don't accidentally allow ../ etc
if ( in_array( '..', $to ) || in_array( '.', $to ) )
{
trigger_error( "my_{$action_word}_file: \$destination does not allow path traversion using /../ or /./", $e_error_code );
}
// make sure we have a filename on $destination
if ( is_dir( $destination ) )
{
// user gave a directory but no filename so use the filename in $source
$to[] = basename( $source );
$destination = '/'.implode( '/', $to );
}
}
if ( file_exists( $destination ) && is_writable( $destination ) )
{
if ( ! $overwrite )
{
trigger_error(
"my_{$action_word}_file: \$destination already exists and I am instructed not to overwrite.",
__internal_my_move_or_copy_file_e_notice );
return false;
}
}
elseif ( is_dir( dirname( $destination ) ) || is_writable( dirname( $destination ) ) )
{
// we can write
}
else // all allowable situations are already passed
{
trigger_error(
"my_{$action_word}_file: $destination directory does not exist or cannot be written to.",
__internal_my_move_or_copy_file_e_error );
}
if ( $is_move )
{
// if we are going to move a file the source also needs to be writable
if ( ! is_writable( $source ) )
{
trigger_error(
"my_{$action_word}_file: Cannot {$action_word} \$source because it cannot be written.",
__internal_my_move_or_copy_file_e_error );
}
$result = rename( $source, $destination );
}
else
{
$result = copy( $source, $destination );
}
// see if what php built in function gave us is acceptible
if ( $result === false )
{
trigger_error(
"my_{$action_word}_file: unexpected failure to {$action_word} \$source to \$destination.",
__internal_my_move_or_copy_file_e_error );
}
// postflight check if the work we did was successful
if ( !file_exists( $destination ) )
{
trigger_error(
"my_{$action_word}_file: unexpected failure to {$action_word} \$destination does not exist after {$action_word} operation.",
__internal_my_move_or_copy_file_e_error );
}
}
else // file does not exists or is unreadable
{
trigger_error(
"my_{$action_word}_file: \$source \"$source\" does not exist or cannot be read.",
__internal_my_move_or_copy_file_e_error );
}
return $result;
}
a source to share