Search code examples
phpwindowslinuxfwrite

Linux Box using PHP writing file to Windows Server Web Share


We have a bunch of linuix and windows servers.

On my windows desktop I can see all the shares.

Using PHP I'm attempting to write a file to a directory on a windows share using the UNC path.

//ServerShare/directory/file.txt

Using fwrite says it successfully wrote the file but the file never exists on the server.

Using opendir function says the directory isn't accessible.

This is the source pretty simple.

 $file_name = "\\SERVER2\Share\CPI\data.txt";

if (!$handle = fopen($file_name, 'w')) {
     echo "Cannot open file ($file_name)";
     exit;
}

// Write $somecontent to our opened file.
$somecontent = "this is a test";
if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

echo "Success, wrote ($somecontent) to file ($file_name)";

fclose($handle);

Any thoughts on what types of permissions need to be set to let a linux box write files to a windows box?


Solution

  • You should be mounting the fileshare to a local directory:

    mount -f smbfs //user@server2/Share/CPI/Data.txt /media/share
    

    Then access /media/share/Share/CPI/Data.txt from your PHP script.

    PHP needs to authenticate to the share, even if it is public, and using fopen or opendir does not do this.