WAMP server
PHP fopen function needs to open file in shared folder //server/folder1/file1.txt
php has SYSTEM user permisions, but shared folder is visible only for userX/password
how can i open this remote file inside php script?
You can specify the username and password in the path: //user:password#server/folder1/file1.txt
I had the exact same problem and Sjoerd's answer didn't work for me on Windows.
I wrote a mounting function in PHP which basically execs:
net use "\\smbserver\share" /user:"myuser" "mypassword" /persistent:no
Then you can normally access paths like \\smbserver\share/path/file.
This works for all functions specified in the options table for file wrappers:
Allows Reading
Allows Writing
Allows Appending
Allows Simultaneous Reading and Writing
Supports stat()
Supports unlink()
Supports rename()
Supports mkdir()
Supports rmdir()
This works perfectly stable on remote shares but didn't work for me when I used localhost with apache as SYSTEM.
Don't forget to unmount afterwards by execing:
net use "\\smbserver\share" /delete /yes
I hope this helps.
Related
I want to to read and write some data to text files from my PHP app. Its a small amount of data. Really just configurations.
My concern is that I have no control over the deployment environment. It will be a mixed bag of servers, mac and windows. They will all be running PHP5.3 and greater. The mac servers will have apache, and the windows servers will be running IIS.
I don't want customers calling with issues related to server setup and or permissions on files/folders.
I am pretty sure that WordPress does this all the time so, I know this is possible to do cleanly. The questions is how? Does anyone have any suggestions, pointers to libraries, or strategies that will help me accomplish my goal.
I ruled out sqlLite for this purpose, because i don't think it is enabled by default on windows and I think it is no longer installed on php5.4 windows by default. My main goal is to be able to persist a small amount of data in such a way that does drive my support costs through the rough the roof.
The way all common big PHP projects do it afaik is simply by reserving a folder for it, and checking its permissions on install.
You could easily make a /gen or /data folder in your webroot, and on install/update check that it:
contains a .htaccess file stating deny from all if the webserver is Apache, or an equivalent method of protection (just file_get_contents via the public URL to test) on other webservers
is_writable (you could also write, read and delete a small sample file to ensure this)
Put your documents in there and it's safe and portable on every platform.
Some sample code:
$docroot = $_SERVER['DOCUMENT_ROOT'];
$dataroot = $docroot.'/data';
$testfile = $dataroot.'/test.txt';
$publicURL = $youHaveThisSomewhere.'/data/test.txt';
if(!is_dir($dataroot))
die("The required data folder is not present at $dataroot");
if(!is_writable($dataroot) || file_put_contents($testfile, 'test') === FALSE)
die("Data path ($dataroot) is not writable, make it so!");
if(file_get_contents($publicURL) !== FALSE)
die("Data path is publicly accessible, go fix it!");
if(!unlink($testfile))
die("I also need delete rights in the data folder!");
die("Installation successful!");
Yeah, but even with Wordpress you have to worry about making certain files and directories writable (chmod, windows file permissions, etc).
And if I'm not mistaking Joomla/Wordpress (one of them at least) also give you the opportunity to enter FTP credentials, so rather than editing the file through the filesystem, it will try to upload the edited version through the FTP server.
What you simply could do is have a config file that must be edited in a text editor. And have the text-file read only for the webserver / application. But then changing one of these setting cannot be done through the website itsself, but needs a person to edit the text file in a text editor.
If you are already using a database, then ONLY the database-settings would have to be in the config file. The rest can be stored in the database.
I think the option you may be looking for involves the following steps:
Create an install file in this install script file:
See if directory is writable
If not ask for FTP credentials
Determine the system type Windows or *Nix bassed
Establish an FTP connection
*nix variants send a CHMOD command Windows send a CACLS command to make the directory writable
Terminate the FTP command
how can i access folders like
\\10.200.0.3\some_folder
user name: user
password: pass
is it possible to map network device then access it using php?
please help me connect to this share and access files there..
Try one of these;
Make sure the IIS worker process has access to that location (IUSR or IIS_IUSRS)
Change the website's application settings in IIS to use credentials from a user account who has access to the location
Since you will be running PHP using IIS/Apache user make sure server user also has permissions to access network shared folder. Once you are sure IIS user does have permissions to access shared folder, you may try listing contents of shared folder by using path such as:
$target_path = '\\\\server\\images\\';
Have a look at this questions, might be helpful:
PHP: upload files to network shared folder
What you're looking for is Server Message Block (SMB) or the open source implementation SAMBA.
To use this in PHP, you could look at smbwebclient.
You might however simply add a drive map to the share on your Windows server, assign it a fixed letter (like Z:) or even simply access \\hostname\folder (escape the backslashes) and use the PHP built in directory commands to read and write the files from that drive. It'll be a helluvalot easier.
I want to copy a zip file from remote to my local system using SCP.
I have a php file where i use php function exec();
if i run upload.php like http://www.abc.com/upload.php.
The zip file should copy to my local linux folder my path is
/var/www/html/mydirectory/
How can i do this ?
You can use PHP's PECL ssh2 extension that provides ssh2_scp_send.
In order to automate any ssh connection like scp you have to set up a pair of auth keys.
This will allow your remote computer to connect to your local computer with out a password prompt. A simple google search will show you how to set this up. The resource I used is http://linuxproblem.org/art_9.html.
The auth key allow the computers to recognize each other and handshake with out a user prompt but remember doing this does provide free ssh access from your remote location to your home computer without a password, so handle permissions carefully.
A better way than an scp if you don't need encryption is to set up a wget on your local computer to grab off your remote computer's web dir.
To me, it seems like you are asking how to download a zip file from your remote web server. In that case, you could simply give the browser the direct path to the zip and let it download it. You can't push a file from the server to the local machine with SCP. Use HTTPS if you're concerned about security. If the zip file is outside of the web directory, you can use PHP to read the file (assuming apache has access to it) and then output it to the browser.
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.
How can I download folder from some ftp server into my server home directory and give to that directory rights (like all files in this directory have all or no rights)?
Not using special libraries if it is possible.
<?php
file_put_contents('./file.txt', file_get_contents('ftp://server/file.txt'));
?>
The FTP server must support passive mode (ref) and your web server must have allow_url_fopen set in the php.ini (ref).
To give rights use chmod('./file.txt', 0777) or whatever rights you need.
I think you may want PHP's functions for FTP such as ftp_nlist and ftp_nb_get:
http://www.w3schools.com/PHP/php_ref_ftp.asp
I also found this resource which looks like a good tutorial as well as usable code:
http://www.raditha.com/php/ftp/pasv.php