Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,处理文件系统函数错误和警告的正确方法是什么?_Php_Exception Handling_Error Handling - Fatal编程技术网

在PHP中,处理文件系统函数错误和警告的正确方法是什么?

在PHP中,处理文件系统函数错误和警告的正确方法是什么?,php,exception-handling,error-handling,Php,Exception Handling,Error Handling,使用时,处理错误的正确方法是什么,例如: 警告:symlink():第XXX行的/path to script/symlink.php中没有此类文件或目录 我通常的方法是在调用文件系统函数之前检查可能产生错误的任何条件。但是,如果命令由于我没有预见到的原因而失败,我如何捕获错误以向用户显示更有用的消息 这是创建符号链接的代码的简化: $filename = 'some-file.ext'; $source_path = '/full/path/to/source/dir/'; $dest_pat

使用时,处理错误的正确方法是什么,例如:

警告:symlink():第XXX行的/path to script/symlink.php中没有此类文件或目录

我通常的方法是在调用文件系统函数之前检查可能产生错误的任何条件。但是,如果命令由于我没有预见到的原因而失败,我如何捕获错误以向用户显示更有用的消息

这是创建符号链接的代码的简化:

$filename = 'some-file.ext';
$source_path = '/full/path/to/source/dir/';
$dest_path = '/full/path/to/destination/dir/';

if(file_exists($source_path . $filename)) {
    if(is_dir($dest_path)) {
        if( ! file_exists($dest_path . $filename)) {
            if (symlink($source_path . $filename, $dest_path . $filename)) {
                echo 'Success';
            } else {
                echo 'Error';
            }
        }
        else {
            if (is_link($dest_path . $filename)) {
                $current_source_path = readlink($dest_path . $filename);
                if ( $current_source_path == $source_path . $filename) {
                    echo 'Link exists';
                } else {
                    echo "Link exists but points to: {$current_source_path}";
                }
            } else {
                echo "{$source_path}{$filename} exists but it is not a link";
            }
        }
    } else {
        echo "{$source_path} is not a dir or doesn't exist";
    }
} else {
    echo "{$source_path}{$filename} doesn't exist";
}  
后续行动/解决方案

正如Sander所建议的,使用
set\u error\u handler()
将错误和警告转化为异常

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}

set_error_handler("exception_error_handler");

try {
    symlink($source_path . $filename, $dest_path . $filename);
    echo 'Success';
}
catch (ErrorException $ex) {
    echo "There was an error linking {$source_path}{$filename} to {$dest_path}{$filename}: {$ex->getMessage()}";
}

restore_error_handler();
使用@operator是另一种解决方案():


我想您应该设置一个抛出异常的errorhandler:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    // see http://php.net/manual/en/class.errorexception.php
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}

set_error_handler("exception_error_handler");
那么您的代码将是:

try {
    symlink(); // when an error occured, it jumps to the catch
} catch (ErrorException $ex) {
    // do stuff with $ex
}

我建议使用广泛使用并经过测试的文件系统操作解决方案Fileysystem组件

我目前没有使用任何框架,因为这是一个简单的脚本。它确实给了我一些关于如何解决问题的想法,谢谢你的链接。你可以将它作为独立的组件使用,而不需要使用框架。每次出现错误都会调用它吗?如果我只想在某些函数上使用,该怎么办?catch只会在发生第一个错误时调用。你可以读一下这个主题,我的意思是如何将它定位到一个特定的函数,后来我找到了
restore\u error\u handler()
:)。
try {
    symlink(); // when an error occured, it jumps to the catch
} catch (ErrorException $ex) {
    // do stuff with $ex
}