在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错误消息:
警告:fopen(Individuals.txt)[function.fopen]:无法打开流:第145行/home/content/b/u/r/burtsweeto/html/ADREImport.php中没有此类文件或目录 array(4){[“type”] =&gt; int(2)[“message”] =&gt; string(118)“fopen(Individuals.txt)[function.fopen]:无法打开流:没有这样的文件或目录”[“file”] =&gt; string(56)“/ home / content / b / u / r / busweeto / html / ADREImport.php”[“line”] =&gt; int(145)}
最后,我尝试将文件放在运行PHP脚本的目录中;它确实在那里正常工作!我只是试图通过在导入之前不必上传大文件来最小化最终用户的持续头痛。
我还没有尝试过任何建议吗?
答案 0 :(得分:1)
添加$file
的完整路径,如下所示:
$file = "C:\\SWSRE\\Individuals.txt";
set_include_path()
和ini_set()
听起来像他们一样;他们调整包含路径,这与所有路径不同。 file_exists()
要求绝对路径或相对于调用它的PHP文件的路径。它不受set_include_path()
或ini_set('safe_mode_include_dir',...)