Reputation: 1
Using php 5.2.17, on a Linux Server. My office production machine is Windows7 Professional with Service Pack 1 installed.
Trying desperately -- and so far, in vain -- to get fopen()
to find and open a .csv file on my local machine in order to import records to an existing MySQL database on the server. Consistently getting a "failed to open stream" error message.
Here is part of the code, with explanatory notes / server responses, including notes on what I have tried:
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);
Here are the final PHP error messages:
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) }
Finally, I have tried putting the file up in the directory where the PHP script is running; and it does work exactly correctly there! I'm just trying to minimize ongoing headaches for the end-user by not having to upload a huge file before doing the import.
Any suggestions that I have not tried?
Upvotes: 0
Views: 2150
Reputation: 44833
Add the full path to $file
, like this:
$file = "C:\\SWSRE\\Individuals.txt";
set_include_path()
and the ini_set()
do what they sound like; they adjust include paths, which is not the same as all paths. file_exists()
expects either an absolute path or a path relative to the PHP file calling it. It is not affected by set_include_path()
or ini_set('safe_mode_include_dir',...)
Upvotes: 1