Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : rename() How do I find the error cause?

Tags:

php

I would like to print out the cause of the error.

error_get_last() doesn't seem to return anything. rename() returns TRUE|FALSE rather than an exception.

if (!rename($file->filepath, $full_path)) {
  $error = error_get_last();
  watchdog('name', "Failed to move the uploaded file from %source to   %dest", array('%source' => $file->filepath, '%dest' => $full_path));
}
like image 900
Interlated Avatar asked Jul 06 '12 10:07

Interlated


People also ask

What is the purpose of the rename () function when creating a file PHP?

The rename() function in PHP is an inbuilt function which is used to rename a file or directory. It makes an attempt to change an old name of a file or directory with a new name specified by the user and it may move between directories if necessary.

How do you rename the file if already exists in PHP?

Solution: We need to rename by appending increment number to the filename if file exists using PHP file_exists() function.

How to rename files with PHP?

PHP rename() Functionrename("images","pictures"); rename("/test/file1. txt","/home/docs/my_file. txt");


1 Answers

First, it's a better practice to add some safety checks before:

if (file_exists($old_name) && 
    ((!file_exists($new_name)) || is_writable($new_name))) {
    rename($old_name, $new_name);
}

Second, you can turn on error reporting:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
like image 133
Nir Alfasi Avatar answered Sep 21 '22 17:09

Nir Alfasi