I'm looking for a way to read the file contents of a file located on a network share. I can use the IP Address of the share host and the share folder to get to the location but I don't know the correct command and syntax to do that file_get_contents? fopen?
$text = fopen('//128.251.xxx.xxx/Common/sample.txt', 'r');
or something like that?
UPDATE*
I also cannot access the file through a browser window although I know the file is located in that exact directory...
The best way (depending on your needs) is to simply mount it on your local machine, and then read from the mount. That lets all the network interaction be abstracted away.
So, in Linux:
mount -t cifs //192.168.XXX.XXX/share /path/to/mount -o user=username,password=password
Then, in php, just access it from the mount point:
$data = file_get_contents('/path/to/mount/path/to/file.txt');
According to PHP Filesystem manual UNC/SMB files should be accessible. Try the following:
\\server\share\file.txt
It may be one of those cases where the // may be mistook as a reference to the root directory. And given this is an SMB path, I would use the traditional backslashes.
Related
I'm trying to implement php script on server created by someone else.
I try echo shell_exec('ls'); and it works.
var_dump(shell_exec('find / -name "tmp.txt"'));
returns null.
However when I try it in ssh console, the output is correct. What can cause it? What can I check
You're trying to locate something in the root directory, which your web server doesn't have permissions to access (by design) - what you're wanting to do would be a security concern, so my suggestion is to place your work in a folder inside your website, owned by the same user that is allowed to access files on your drive, typically www-data.
Perhaps you don't want to read the root directory of the server itself, and you just want to read a file in the same directory as the website. Switching out the / for a . will access the current directory, not the base directory:
var_dump(shell_exec('find . -name "tmp.txt"'));
Or, you could try an absolute path:
var_dump(shell_exec('find /path/to/files -name "tmp.txt"'));
This is uncommon, but you may need to point to an absolute path for find as well. You can test if you need to do this by running var_dump(shell_exec('find .')); and if it has no output then you will probably have to. Use /usr/bin/find instead if this is the case.
Read more about how to set permissions
Apache: File and Directory Ownership and Permissions for Web Content
A reason for this can be a missing PATH environment variable.
Please check getenv('PATH') and try to use the absolute path to find. A usual place is /usr/bin/find or /bin/find.
var_dump(shell_exec('/usr/bin/find / -name "tmp.txt"'));
i have program, the log file address like that
C:\Users\Administrator\AppData\Roaming\program-folder\Logs
and now i want read this log file (text) with php
how can assign address to open this file?
PHP run by Apache cannot access files outside of the site root for obvious security reasons. If you can't move the log file, you could look to use a symlink to make it accessible.
Based on the path you've given I'm assuming you're using Windows. Here's a guide to symbolic links in Windows.
I wrote a PHP script, which is creating some files.
After it finished i want it to open a folder, where the created files stored.
exec("explorer C:\\test");
Unfortunately it isn't open the folder at all.
I check it in cmd as well where it is working.
I also tried this:
shell_exec("explorer C:\\test");
Any hint or advise is greatly appriciated.
This can be done if your localhost is on your computer with windows operating system (I use xampp). Any cmd command will work with exec();
So for your question:
$path = "C:\your path";
exec('start "" "'.$path.'"');
Will open your folder in windows.
Why not use PHP's directory functions?
http://php.net/manual/en/function.dir.php
http://php.net/manual/en/function.readdir.php
I'm guessing that PHP can't open a GUI program, such as explorer, as it may not be able to access the user's display. Just my theory anyway. You could build a folder GUI using PHP and the aforementioned functions.
In php, I need to read a file that has no read access (the file permissions are -rw-r-----).
Changing the file's permissions isn't possible. The file sits on a local server.
Various methods I've tried in PHP don't work (file_get_contents, fopen, and curl) and maybe that's to be expected if that last read bit isn't set. Is that because the web server is being blocked access?
If that's the case then why is it that Firefox can read the file directly (using file://) as does curl from a shell? About to write an external python script that can read the file... what am I missing here?
It depends what user owns the file, and what user PHP/Apache is running as. You could check it by running whoami from PHP. If you can't change any part of the permissions/owner on the file, nor the Apache user, then, well, you're stuffed sorry.
I have a apache dedicated server with lost of websites.
I also have a red5 installation on the server.
What I want to know how to do is perform file functions - specifically unlink() - on files held in the RED5 directory within the root server dir.
I can move files with this:
copy ("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename, $strDestination);
but
unlink("http://www.parttimepornstar.com:5080/echo/streams/".$strFilename);
...won't work...
Any ideas what I'm doing wrong?
Thanks.
You need to check the permission of the file that you are trying to delete. Apache should (hopefully) not be running as root and therefore cannot delete any files that it does not have permissions for.
You should also be vary wary of security. Allowing an unchecked variable to be used in the end of a copy() or unlink() call could potentially give a user access to your entire filesystem. Take a look at basename.
I suspect you need to use absolute file paths rather than urls/relative paths. Also if you want to delete from root dir, you need to specify that too. Try doing something like below:
unlink($_SERVER['DICUMENT_ROOT'] . '/RED5/' . $yourfiles);
You are addressing the files via HTTP, which you can't use to delete files.
You need to specify a filesystem path such as /etc/httpd/sitename/file.php