I have a network drive mapped and am trying to read a txt file from it to no avail. I can read anything from my C drive but can't figure out how to read from another drive. Here's my code:

$myFile = "O:\path\to\my\file\File.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;

    i found using the just the filesystem fopen and file_get_contents work great for these kinds of situations. i also struggles with the fread function could not get it to work right ever!.

    
    $filename=''; //path to file.
    
    $open=fopen($filename , 'rb');
    
    
    {
     // some check here.
    }
    
    $getcont=file_get_contents($filename);
    
    {
    // now you have all contents in a string know you can use one of many string functions at hand.
    }
    
    

    also if you on windows use 'rb'

    and you missing some double quotes around your echo "" value.

      Your problem is here:

      $myFile = "O:\path\to\my\file\File.txt"

      The backslash character has special meaning when used in double quoted strings. Either escape each backslash with a backslash (e.g. '\' instead of '\'), or simply use forward slashes instead.

        a month later

        What's 'rb'?

        I'm using forward slashes now and it doesn't seem to be working...

          DKY wrote:

          What's 'rb'?

          It's the reading mode which explicitly states that the file should be read in binary mode (versus text-mode, which might do some translations on the data that you don't want). The manual page for [man]fopen/man explains this and much more.

          DKY wrote:

          I'm using forward slashes now and it doesn't seem to be working...

          Can you show us the code you're using now? Also, if it's not working, what PHP error message is being returned?

          Also... from your last post, I'm guessing that this issue isn't resolved? I've un-marked this thread as resolved, so let us know if you've already fixed the problem or if you're still working on it.

            LOL, yeah this is 'un'resolved. I thought it was resolved but it no workey.
            My code is below, I'm in essence reading in a txt file from the network drive and putting into a MySQL database. I get no error, I just get a blank page but I know these two things:

            1) There is a file in that folder (when I log onto the server remotely and paste that path into the address bar of explorer it pulls up the file).
            2) If I use the exact same code but have a file on my c drive instead, it updates the database. All I'm changing is the filename variable.

            <?PHP
            
            require("connect.php");
            
            //$filename = "C:/xampp/htdocs/ScheduledTasks/Downloads/UPTIMEPS.TXT";
            $filename = "O:/Common/Common-Parts/JK/Uptime/UPTIMEPS.TXT";
            
            if (file_exists($filename)) {
            
            $connectmysql = mysql_connect($host,$user,$password) or die ($cantconnectmysqlmessage);
            $db = mysql_select_db($databaseinv,$connectmysql) or die ($cantconnectdatabasemessage);
            
            $delete_query = "DELETE FROM tbluptimeinv ";
            		if (!($delete_result = mysql_query($delete_query))){
            			$mcrmessage = mysql_error();
            			echo "$mcrmessage<br>";
            			die();
            		}
            
            // Central Standard Time
            date_default_timezone_set('America/Chicago');
            $timenow = date("Y-m-d H:i:s");
            // Central Standard Time
            
            /*--- READING IN THE FILE ---*/
            
            $file_handle = fopen($filename, "r");
            while (!feof($file_handle)) {
               $line = fgets($file_handle);
            
            IF(TRIM(SUBSTR($line,0,17))<>""){$NAV_PN = TRIM(SUBSTR($line,0,17));}ELSE{$NAV_PN = "";}
            IF(TRIM(SUBSTR($line,17,6))<>""){$ONHUPTWC = TRIM(SUBSTR($line,17,6));}ELSE{$ONHUPTWC = "";}
            IF(TRIM(SUBSTR($line,24,6))<>""){$ONHUPTRN = TRIM(SUBSTR($line,24,6));}ELSE{$ONHUPTRN = "";}
            IF(TRIM(SUBSTR($line,31,6))<>""){$ORDUPTWC = TRIM(SUBSTR($line,31,6));}ELSE{$ORDUPTWC = "";}
            IF(TRIM(SUBSTR($line,38,6))<>""){$ORDUPTRN = TRIM(SUBSTR($line,38,6));}ELSE{$ORDUPTRN = "";}
            echo $NAV_PN;
            
            
               $insert_query = "INSERT INTO tbluptimeinv
            		(TIMESTAMP, NAV_PN, ONHUPTWC, ONHUPTRN, ORDUPTWC, ORDUPTRN)
            		VALUES
            		('$timenow', '$NAV_PN', '$ONHUPTWC', '$ONHUPTRN', '$ORDUPTWC', '$ORDUPTRN')";
            
            	if (!($insert_result = mysql_query($insert_query)))
            	{
            		$mcrmessage = mysql_error();
            		echo $mcrmessage . "<br><br>" . $insert_query;
            		die();
            	}
            
            }
            header("Location: http://whqpc-001588/SMR/EComm.php");
            fclose($file_handle);
            }
            ?>
              DKY wrote:

              (when I log onto the server remotely and paste that path into the address bar of explorer it pulls up the file)

              This doesn't necessarily mean that PHP has access to the same networked drive as what you're seeing. For example, if this "O:" network drive was mapped in a user logon script, it's very possible that you see the "O:" drive when you remotely login, but the local system (where PHP is running) doesn't see it.

              First, is display_errors (and/or log_errors, whichever is your preference) set to On and error_reporting set to E_ALL? If PHP is having trouble accessing a file, it should be outputting an E_WARNING level (or above?) error message.

              Also, try adding this to your code (just after you define $filename might be a good place) just to generate some debugging information:

              echo "file_exists: "; var_dump( file_exists($filename) ); echo "<br>\n";
              echo "is_readable: "; var_dump( is_readable($filename) ); echo "<br>\n";
              echo "fopen: "; var_dump( fopen($filename) ); echo "<br>\n";

                I have to step away for about an hour but just wanted to throw out there that all pcs in our network have drives mapped by a user logon script. Is there a way for the pc running apache to see the network drive eventhough it wasn't visible until the logon script?

                  DKY wrote:

                  Is there a way for the pc running apache to see the network drive eventhough it wasn't visible until the logon script?

                  I can't think of any easy way to do that, no.

                  You could always hard-code the UNC path to the network share (e.g. "//server/sharename/path/to/file.txt") instead of relying on the mapped drive always being available to the Apache process.

                  Speaking of that last bit, also try adding this to the debugging code above:

                  echo "is_dir(O:/): "; var_dump( is_dir('O:/') ); echo "<br>\n";
                  echo "is_readable(O:/): "; var_dump( is_readable('O:/') ); echo "<br>\n";

                    Hmmm, I tried hard coding the UNC path to no avail. I get this:

                    file_exists: bool(false)
                    is_readable: bool(false)
                    fopen:
                    Warning: fopen() expects at least 2 parameters, 1 given in C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php on line 11
                    bool(false)
                    is_dir(O:/): bool(false)
                    is_readable(O:/): bool(false)

                      Okay, the fopen() warning was my bad - silly me forgot to specify the file mode.

                      However, it would appear from those results that 'O:/' isn't going to work with your current setup.

                      Can you show us the UNC path you tried instead?

                        I did this

                        $filename = "//canfsw03/groups1/Common/Common-Parts/JK/Uptime/UPTIMEPS.TXT";

                          Okay, what happens if you add this just under that line (using the full UNC path):

                          var_dump( file_get_contents($filename) );

                            Okay, what happens if you add this just under that line (using the full UNC path):

                            var_dump( file_get_contents($filename) );

                              I get

                              Warning: file_get_contents(//canfsw03/groups1/Common/Common-Parts/JK/Uptime/UPTIMEPS.TXT) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php on line 8
                              bool(false) file_exists: bool(false)
                              is_readable: bool(false)
                              fopen:
                              Warning: fopen() expects at least 2 parameters, 1 given in C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php on line 12
                              bool(false)
                              is_dir(O:/): bool(false)
                              is_readable(O:/): bool(false)

                                It's possible that it's a permissions issue (though I've always gotten a permissions-related error message in the past).

                                Do you know how to execute a PHP script from the CLI? If so, try executing this same script from the CLI when you're logged in as a user that has access to this file. See if you get the same error messages.

                                  took me a while to figure out what a CLI was but I did it. Here's what I get when I run the file from internet explorer on the server (via remote desktop)

                                  Warning: file_get_contents(//canfsw03/groups1/Common/Common-Parts/JK/Uptime/UPTIMEPS.TXT) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php on line 8
                                  bool(false) file_exists: bool(false)
                                  is_readable: bool(false)
                                  fopen:
                                  Warning: fopen() expects at least 2 parameters, 1 given in C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php on line 12
                                  bool(false)
                                  is_dir(O:/): bool(false)
                                  is_readable(O:/): bool(false)

                                  and here's what I get when I run the file in the CLI from the server via remote desktop

                                  C:\Documents and Settings\u00jbk7\Desktop\InventoryStuff>C:\xampp\php\php.exe -f
                                  "C:\xampp\htdocs\ScheduledTasks\Uptime_SAS-MySQL.php"

                                  Warning: file_get_contents(//canfsw03/groups1/Common/Common-Parts/JK/Uptim
                                  e/UPTIMEPS.TXT): failed to open stream: No such file or directory in C:\xampp\ht
                                  docs\ScheduledTasks\Uptime_SAS-MySQL.php on line 8
                                  bool(false)
                                  file_exists: bool(false)
                                  <br>
                                  is_readable: bool(false)
                                  <br>
                                  fopen:
                                  Warning: fopen() expects at least 2 parameters, 1 given in C:\xampp\htdocs\Sched
                                  uledTasks\Uptime_SAS-MySQL.php on line 12
                                  bool(false)
                                  <br>
                                  is_dir(O:/): bool(true)
                                  <br>
                                  is_readable(O:/): bool(true)
                                  <br>

                                  C:\Documents and Settings\u00jbk7\Desktop\InventoryStuff>pause
                                  Press any key to continue . . .

                                    Well that's not what I had expected... hm. Are you positive that path is correct? For example, if you copy and paste this:

                                    \\canfsw03\groups1\Common\Common-Parts\JK\Uptime\UPTIMEPS.TXT

                                    into either a command prompt window or just the Run box (Start->Run), does the file open correctly?

                                      well i went through your script line by line on

                                      change $filehandle=fopen($filename , 'rb');

                                      windows is really picky and only will read the file in binary. (Read!)

                                      and next your path has to be wrong!.

                                      brad no offence but windows doesn't use file permissions like linux or OSX

                                      
                                      require("connect.php");
                                      
                                      
                                      
                                      $filename = "O:/Common/Common-Parts/JK/Uptime/UPTIMEPS.TXT";
                                      
                                      
                                      
                                      if (file_exists($filename)) 
                                      
                                      {
                                      
                                      $connectmysql = mysql_connect($host,$user,$password) or die ($cantconnectmysqlmessage);
                                      $db = mysql_select_db($databaseinv,$connectmysql) or die ($cantconnectdatabasemessage);
                                      
                                      }
                                      
                                      
                                      else { trigger_errror('failed to open file' .  $filename . 'file path is wrong or file permissions set.' );}
                                      
                                      
                                      
                                      $delete_query = "DELETE FROM tbluptimeinv ";
                                      
                                      
                                          if (! $delete_result = mysql_query($delete_query)){
                                              $mcrmessage = mysql_error();
                                              echo "$mcrmessage<br>";
                                              die();
                                      
                                      }
                                      
                                      
                                      // Central Standard Time
                                      date_default_timezone_set('America/Chicago');
                                      $timenow = date("Y-m-d H:i:s");
                                      // Central Standard Time
                                      
                                      /*--- READING IN THE FILE ---*/
                                      
                                      $file_handle = fopen($filename, "r");
                                      while (!feof($file_handle)) {
                                         $line = fgets($file_handle);
                                      
                                      IF(TRIM(SUBSTR($line,0,17))<>""){$NAV_PN = TRIM(SUBSTR($line,0,17));}ELSE{$NAV_PN = "";}
                                      IF(TRIM(SUBSTR($line,17,6))<>""){$ONHUPTWC = TRIM(SUBSTR($line,17,6));}ELSE{$ONHUPTWC = "";}
                                      IF(TRIM(SUBSTR($line,24,6))<>""){$ONHUPTRN = TRIM(SUBSTR($line,24,6));}ELSE{$ONHUPTRN = "";}
                                      IF(TRIM(SUBSTR($line,31,6))<>""){$ORDUPTWC = TRIM(SUBSTR($line,31,6));}ELSE{$ORDUPTWC = "";}
                                      IF(TRIM(SUBSTR($line,38,6))<>""){$ORDUPTRN = TRIM(SUBSTR($line,38,6));}ELSE{$ORDUPTRN = "";}
                                      echo $NAV_PN;
                                      
                                      
                                         $insert_query = "INSERT INTO tbluptimeinv
                                              (TIMESTAMP, NAV_PN, ONHUPTWC, ONHUPTRN, ORDUPTWC, ORDUPTRN)
                                              VALUES
                                              ('$timenow', '$NAV_PN', '$ONHUPTWC', '$ONHUPTRN', '$ORDUPTWC', '$ORDUPTRN')";
                                      
                                          if (!($insert_result = mysql_query($insert_query)))
                                          {
                                              $mcrmessage = mysql_error();
                                              echo $mcrmessage . "<br><br>" . $insert_query;
                                              die();
                                          }
                                      
                                      }
                                      header("Location: http://whqpc-001588/SMR/EComm.php");
                                      fclose($file_handle);
                                      }
                                      ?> 
                                      
                                      
                                        jgetner wrote:

                                        brad no offence but windows doesn't use file permissions like linux or OSX

                                        In the sense that there is no octal CHMOD mask, sure, but Windows (read: NTFS) does have its own complex set of ACL rules/permissions, as well as "owners" (e.g. what the *nix 'chown' changes) and group membership and all of that fun stuff.

                                        EDIT:

                                        jgetner wrote:

                                        windows is really picky and only will read the file in binary

                                        Not sure which Windows you're using, but XP and 7 both read text files just fine without binary mode for me. So.. no offense, but that statement isn't correct. :p

                                        Also:

                                        jgetner wrote:

                                        your path has to be wrong!

                                        Er... what do you mean? Why does it have to be wrong?