在Linux服务器上使用PHP5.2.17。我的办公室生产机器是Windows7 Professional,安装了Service Pack 1。
拼命尝试——到目前为止,徒劳地——让fopen()找到并打开我的本地机器上的.cv文件,以便将记录导入到服务器上现有的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错误消息:
警告:fopen(personals.txt)[function.fopen]:无法打开流:145行/home/content/b/u/r/burtsweeto/html/ADREImport.php中没有此类文件或目录
数组(4){[“type”]=>int(2)[“message”]=>string(118)“fopen(personals.txt)[function.fopen]:无法打开流:没有这样的文件或目录“[“file”]=>string(56)”/home/content/b/u/r/burtsweeto/html/adrimport.php“[“line”]=>int(145)}
最后,我试着把文件放在PHP脚本运行的目录中;它确实在那里正确工作!我只是想通过在导入之前不必上传一个巨大的文件来尽量减少最终用户持续的麻烦。
有没有我没有试过的建议?

最佳答案

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

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

set_include_path()ini_set()执行它们听起来像的操作;它们调整include路径,这与所有路径不同。file_exists()需要绝对路径或相对于调用它的PHP文件的路径。它不受set_include_path()ini_set('safe_mode_include_dir',...)的影响

关于php - 存在的本地文件上的PHP fopen(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20624941/

10-12 00:43
查看更多