PHP停止1行代码的警告

 能然然520 发布于 2023-02-03 10:58

我有以下应该存在的警告,但是有没有办法在不实际禁用警告的情况下阻止它们写到页面?

警告:

警告:file_get_contents(E:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(F:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(G:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(H:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(I:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(J:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(K:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(L:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(M:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(N:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(O:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

警告:file_get_contents(P:/connected.txt):无法打开流:第19行的C:\ xampp \ htdocs \ ppa \ test.php中没有此类文件或目录

PHP功能:

//Check if the connection file is present on each drive
function scanDrives() {
    //Possible drives
    $letters = "DEFGHIJKLMNOP";
    $letters = str_split($letters);

    $code = md5("FMbHSBTMTXhu3TWp");

    //Check for a certain file on each device
    foreach($letters as $x) {
        $file = file_get_contents($x.":/connected.txt"); //This is what causes the error as the file can't be found.
        if ($file != false) {
            $file_code = split(":", $file);
            //Check if the file has the correct pass code
            if($file_code[0] == $code) {
                //Successful, return pass value and device letter and set name
                echo (1).",".$x.",".$file_code[1];
            }
        }
    }
}

谢谢。

1 个回答
  • 不要只是使用盲目打开文件file_get_contents。检查它是否首先存在。

    if(file_exists($x.":/connected.txt")){
        $file = file_get_contents($x.":/connected.txt");
        // ...
    }
    

    2023-02-03 11:01 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有