Skip to main content
 首页 » 编程设计

存在的本地文件上的 PHP fopen()

2023年11月22日72softidea

在 Linux 服务器上使用 php 5.2.17。我的办公室生产机器是安装了 Service Pack 1 的 Windows7 Professional。

拼命尝试 - 到目前为止,徒劳无功 - 让 fopen() 在我的本地机器上找到并打开一个 .csv 文件,以便将记录导入到现有的 MySQL 数据库服务器。始终收到“无法打开流”错误消息。

这是代码的一部分,带有解释性注释/服务器响应,包括我尝试过的注释:

ini_set('track_errors', 1);   // Set just to make sure I was seeing all of the rrror codes 
ini_set ('user_agent', $_SERVER['HTTP_USER_AGENT']); // Tried after reading a note; no effect 
 
error_reporting(E_ALL);            // Again, just to make sure all error codes visible! 
echo(get_include_path()."<br />"); // Initially returns: .:/usr/local/php5/lib/php 
set_include_path("C:\SWSRE\\");    // Have tried with BOTH forward and backslashes, BOTH single and doubled, in every conceivable combination! 
ini_set('safe_mode_include_dir',"C:\SWSRE");  // Ditto here for slashes! 
echo(get_include_path()."<br />");  //NOW echoes "C:\SWSRE\" 
clearstatcache();  // Just in case this was a problem -- added after reading note @ php.net 
 
$file = "Individuals.txt";   // This absolutely DOES exist locally in C:\SWSRE\ (29Mb) 
// Inserted following tests to see if it even SEES the file.  It does NOT. 
if (file_exists("Individuals.txt")) { 
    echo("File EXISTS!!!<br />"); 
} else { 
    echo("File does NOT exist!<br />");  // Echoes "File does NOT exist!" 
} 
if(is_readable($file)) { 
    echo 'readable' ; 
} else {  
    echo 'NOT readable!<br />';  // Echoes "NOT readable!" 
 
} 
if(is_writable($file)) { 
    echo 'writable ' ; 
} else {  
    echo 'NOT writable!<br />';  // Echoes "NOT readable!" 
} 
 
$handle = fopen("Individuals.txt", "r+", TRUE); 

以下是最终的 PHP 错误消息:

Warning: fopen(Individuals.txt) [function.fopen]: failed to open stream: No such file or directory in /home/content/b/u/r/burtsweeto/html/ADREImport.php on line 145 array(4) { ["type"]=> int(2) ["message"]=> string(118) "fopen(Individuals.txt) [function.fopen]: failed to open stream: No such file or directory" ["file"]=> string(56) "/home/content/b/u/r/burtsweeto/html/ADREImport.php" ["line"]=> int(145) }

最后,我尝试将文件放在运行 PHP 脚本的目录中;它在那里确实可以正常工作!我只是想通过在导入之前不必上传巨大的文件来尽量减少最终用户的持续头痛。

有什么我没有尝试过的建议吗?

请您参考如下方法:

将完整路径添加到 $file,如下所示:

$file = "C:\\SWSRE\\Individuals.txt";  

set_include_path()ini_set() 就像它们听起来的那样;他们调整 include 路径,这与 all 路径不同。 file_exists() 需要绝对路径或相对于调用它的 PHP 文件的路径。它不受 set_include_path()ini_set('safe_mode_include_dir',...)

的影响