Linux Box using PHP writing file to Windows Server Web Share - php

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?

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.

Related

How to open a ZIP using remote url?

I want to open a ZIP file by passing a remote URL (http://www.example.com/file.zip or http://localhost/wordpress/wp-content/uploads/file.zip) instead of a file location (C:\wamp\www\wordpress\wp-content\uploads\file.zip)
This constructor works fine for a file location but not for a remote url of a file. How does one open a file using a remote URL for this scenario?
public function __construct($file = false)
{
if ($file && is_file($file)) {
//$file="C:\wamp\www\wordpress\wp-content\uploads\file.zip" here
$this->open($file);
$this->fileName = basename($this->filePath = $file);
} else {
throw new \Exception($file . " not a regular file");
}
}
The safest way is to
download the file
This is super easy if allow_url_fopen is enabled: file_get_contents() accepts remote URLs. If that's not enabled, use cURL or a Wordpress HTTP helper to download it.
save it locally
Also super easy, with file_put_contents(). The /tmp folder is probably writable for you. On Windows, I don't know where the tmp folder lives.
open it like any other
As you would a local ZIP archive, with ZipArchive::open() or your nameless class
just use php fopen function
http://php.net/manual/en/function.fopen.php
$handle = fopen("http://www.example.com/", "r");
I have used this to get the contents of a webpage but not a zip file - not sure if that will work but it did well for me. $contents definitely worked for text.
// For PHP 5 and up
$handle = fopen("https://www.thesiteyouwant.com/the_target_file.ext", "r");
$contents = stream_get_contents($handle);
http://php.net/manual/en/function.stream-get-contents.php

file exists but I cannot open it using php

I can open and write in file using vim command but i cant open it using PHP
$myFile = "v.txt";
if(!file_exists($myFile)){
print 'File not found';
}else if(!$fh = fopen($myFile, 'w+')){
print 'Can\'t open file \n';}.
else{
print 'Success open file';
}
Do a
ls -l
on the directory that contains the file. Make sure the user that's running your web server has access read rights to the file

php cli reading files and how to fix it?

While using PHP cli in ubuntu i got apache as web server i cant read the file which i can while using it in the browser.
And about the error it just shows nothing and the next like i just used this code for my testing purposes :
<?
$handle = fopen("testFile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
if(!is_readable($handle)
{
die("Not readable");
}
fclose($handle);
}
?>
How do i fix that ?
EDIT :
After removing the '#' before fopen i got the following error
fopen(testFile.txt): failed to open
stream: No such file or directory in
/var/www/log/ka.php on line 2
When you run your script through CLI, it uses your shell's working directory, in opposition to the file's directory (which Apache hands as the current directory). This is important for you, since your fopen call depends on a relative path; and relative paths are resolved relatively to the working directory, not to the script.
To have your script behave like it does with Apache, you either need to cd /var/www/log prior to running the php command, or add this at the beginning of your PHP script:
chdir(dirname(__FILE__));

fopen unable to open a video file located on server

I am trying to read a video file uploaded on server using fopen and fread in php but fopen returns "unable to open file".
//test.php
<?php
$file=fopen("abc.mov","r") or exit("Unable to open file!");
?>
abc.mov exists in the same folder where test.php is located on the server i.e, at the same hierarchy.
I don't why it isn't able to read the file.
Please help.
This probably isn't a real problem with PHP or your file. This is most likely a problem with the permissions of the file. There are three things you can try here(probably more I don't know of). One, do this somewhere before the fopen in your script:
chmod("abc.mov", 0777);
Then echo fileperms(), just to check(take out after debug):
echo fileperms("abc.mov");
And lastly, before calling fopen, make sure that is_readable and file_exists return true:
if(file_exists("abc.mov") and is_readable("abc.mov")) {
$file = fopen("abc.mov","r") or exit("Unable to open file!");
}
else die("File isn't readable, or maybe doesn't even exist!");
Note: I would be using file_get_contents() and file_put_contents() rather than fopen.
Hope this helps!
Check your file access permissions to make sure you've got access to the file from PHP.

What is the best way to move files from one server to another with PHP?

I want to setup a CRON that runs a PHP script that in turn moves XML file (holding non-sensitive information) from one server to another.
I have been given the proper username/password, and want to use SFTP protocol. The jobs will run daily. There is the potential that one server is Linux and the other is Windows. Both are on different networks.
What is the best way to move that file?
If both servers would be on Linux you could use rsync for any kind of files (php, xml, html, binary, etc). Even if one of them will be Windows there are rsync ports to Windows.
Why not try using PHP's FTP functions?
Then you could do something like:
// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
Why not use shell_exec and scp?
<?php
$output = shell_exec('scp file1.txt dvader#deathstar.com:somedir');
echo "<pre>$output</pre>";
?>
I had some similar situation.
After some tries, I did some thing different
We have 2 servers,
a (that have the original files)
b (files should moved to it)
And for sure the data is NOT sensitive
Now in server a I made a file to do the following when called:
1. Choose the file to move
2. Zip the file
3. Print the .zip file location
4. Delete the .zip file (and the original file) if delete parameter passes
In the server b the file should do:
1. Call the file on the a server
2. Download the zip file
3. Unzip and copy it to the proper location
4. Call the delete function on server a
This way I have more control on my functions, tests and operations!

Categories