Php reading a text file in Linux unbutu

I have built this piece of code using my Windows test rig and works fine but moving it onto my ubuntu web server i just get a white screen and nothing else.

My Code:

<?php
// full path to text file
define("TEXT_FILE", "/var/www/html/SupportDesk/testfile.txt");
///home/webserver/Desktop/testfile.txt");
//define("TEXT_FILE", "afp://[email protected]/Logs/Test/testfile.txt");
// number of lines to read from the end of file
define("LINES_COUNT", 5);

function read_file($file, $lines) {
    echo $file;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}

$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {

    $my_array3 = preg_split("/[ \s,]/",$line);
    print_r($my_array3);
    
    $result = preg_grep("/Total-Updates:/i", $my_array3);
       foreach ($result as $element) {
      echo $element . "<br>";
       }

    $result1 = preg_grep("/Important:/i", $my_array3);
      foreach ($result1 as $element1) {
      echo $element1 . "<br>";
      }
    
    $result2 = preg_grep("/Optional:/i", $my_array3);
        foreach ($result2 as $element2) {
      echo $element2 . "<br>";
      }
}    
?>

I believe its a permission error possibly but any help would be appreciated on this.

If you’re just seeing a white screen and no error messages, it could be due to a variety of issues, including file permissions. However, you’d want to first ensure that your PHP is set up to show error messages so you can debug more effectively.

let’s set PHP to display the error message

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Put the above lines at the top of your PHP script save the file and try again.

If it is a permissions problem, you will need to adjust the permissions on the file so that the user running the PHP script has access to it. You can do this using the chmod command in Linux, like this:

chmod 644 /var/www/html/SupportDesk/testfile.txt

you can do it manually too.
good luck

Ok that has helped a lot, but if i wanted to access a SMB share mapped on my linux machine using Connect to server and typing SMB://IP-ADDRESS what would be the path i would need to access in PHP script the smb connection is smb://IP_ADDRESS/logs/Test/TestFile.txt

This is REALLY frustrating a have a LAMP setup on my Ubuntu box and i cannot access the file share via my php code its not the code as it works fine if i want to access a local file. Its not the NAS as i can access my logs via aft smb nfs all ok from connect to server.

i can only think its the LAMP Apache or PHP setup as contacting the nas i get cannot open stream I didn’t think accessing a system text file be so hard as in Windows it would be simple can anyone assist as its driving me nuts

Accessing an SMB share directly from a PHP script running on a web server can be tricky for various reasons, such as permissions and limitations of PHP itself. However, you may be able to do this by mounting the SMB share to your filesystem.

After mounting the SMB share on your Ubuntu machine, you can access it just like any other local directory. Here is how you can do it:

  1. First, you need to install cifs-utils which allows you to mount SMB shares:
sudo apt update
sudo apt install cifs-utils

  1. Create a directory where you want to mount the SMB share:
sudo mkdir /path/to/mount/point

  1. Mount the SMB share:
sudo mount -t cifs //server-address/share-name /path/to/mount/point -o username=yourusername,password=yourpassword

Make sure to replace //server-address/share-name with the actual address of the SMB share, and /path/to/mount/point with the path where you want to mount the SMB share. Also replace yourusername and yourpassword with your actual username and password.

  1. Check if the share is properly mounted:
ls /path/to/mount/point

Now, you should be able to access the files on the SMB share as if they were in a local directory from your PHP script. So, if your file is located at smb://IP_ADDRESS/logs/Test/TestFile.txt , after mounting smb://IP_ADDRESS/logs/ to /path/to/mount/point/ , you can access your file in the PHP script like this:

$file_content = file_get_contents("/path/to/mount/point/Test/TestFile.txt");

Please note that the mount point is temporary and will be lost after reboot. If you want it to persist through reboots, you need to add it to your /etc/fstab file. This can have its own security implications and should be done carefully.

Warning:
Also, please be aware that the SMB protocol might not be the most performant way to access files for a web application. If you need to access the files frequently or need high performance, you might want to consider other ways of synchronizing the files between the machines, such as rsync or scp.

As always, ensure your SMB connection is secure and access to sensitive data is properly limited, especially if this server is exposed to the internet.

Hope this info helps.

Sponsor our Newsletter | Privacy Policy | Terms of Service