2016-11-12 70 views
-1

我现在面临的问题与此代码:无法读取文件中的PHP

<?php 

$files = scandir("D:/Dummy"); 
foreach($files as $file) { 
    $filenam = $file; 
    $path_to_file = $filenam; 
    $file_contents = file_get_contents($path_to_file); 

    echo "Hello ".$filenam; 
    $printFileName=""; 

    if(strpos("9222339940", $file_contents) === false) 
    { 
     $printFileName=$filenam." "; 
    } 
} 

echo $printFileName;  
?> 

基本上,我写了这个代码进行扫描目录,并从每个文件中的所有文件,我需要替换手机号码。但由于某种原因,我无法运行脚本。它抛出错误:

file_get_contents(name of the file) failed to open stream. No such file or directory error.

+0

那么为了帮助你解决你的“出于某种原因”的问题,首先检查你的$文件是否有用。当你分配了var_dump($ files)后,你看到了什么?他们都是文件或文件和文件夹。 php.net怎么说scandir? – TimBrownlaw

回答

1

PHP的scandir()功能只在目录中返回文件的基本名称。也就是说,如果您的目录D:\ Dummy包含文件test.txt,则scandir()将不会返回完整路径D:\ Dummy \ test.txt,但仅返回test.txt。所以PHP进程不会找到该文件,因为您需要提供文件的完整路径。