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
Related
I have a REALLY strange thing happening! When I view a file (within the "Program Files (x86)" folder tree) in my file manager it has one content, but when I retrieve it through PHP CLI script using file_get_contents() it has different content (with some additional lines I added through the script earlier) - except if I run the CLI script in a prompt with admin rights, then I see the same content. How on earth is it possible that the same file can have different content based on the permissions of the user accessing the file? Is that really possible, and if so where can I find more information on how it works? I've never heard of such a thing in my 25+ years of computing and programming experience...
I have quatro-checked that the path is the same and checked in all kinds of ways that there isn't something else playing a trick on me - but I just can't find any possible explanations!
I'm running Windows 10.
32-bit applications that do not have a requestedExecutionLevel node in their manifest are assumed to be UAC-unaware and if they try to write to a privileged location in the file system or registry (when the process is not elevated) the write operation is virtualized. Virtualized files are stored in %LocalAppData%\VirtualStore.
Manually delete the file in the virtual store and then edit the ACL/security of the file if you need to write to it from your script as a standard user...
recently, I have deployed a web server configuration with Apache, PHP-FPM, and MariaDB. it was running smoothly until my colleague deployed laravel 4 app on it with image uploading capability.
The problem was I have disabled PHP's chmod() function along with system() etc functions, while the script for uploading images uses chmod() and umask() (my colleague said that this is the default behavior).
I know that there is a debate over the danger of chmod wih PHP like this site that tells it is harmless, and this post that tells that a server should never be 777
what I don't understand is, why do you need to chmod() something when the PHP process can read the uploaded file properly even without chmod()? it can even create folders and delete folders without chmod() as those files and folders are created with the correct permission for PHP process.
tl;dr
so that brings to my question, is it true that file uploading in laravel 4 need chmod() to function properly? and why?
edit
for those who like to know the code (this code is found in vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/UploadedFile.php):
$target = $this->getTargetFile($directory, $name);
if (!#move_uploaded_file($this->getPathname(), $target)) {
$error = error_get_last();
throw new FileException(sprintf('Could not move file "%s" to "%s" (%S)', $this->getPathname(), $target, strip_tags($error['message'])));
}
#chmod($target, 0666 & ~umask());
I can't speak for the Laravel's reasoning behind this. It does looks like it is built into their functionality, but a quick search on this brought up comments under http://php.net/manual/en/function.move-uploaded-file.php.
The comments suggest that after moving uploaded files with move_uploaded_file, the permission is set to 0600. This would be fine if the web server and PHP process were running under the same desired user, but not if it relied on the group or a web server running under a generic user like apache or nobody. So my assumption would be Laravel did this for compatibility with the latter systems as 0600 means rw for the user and no access to the group or others.
I need to copy a file from one computer to another computer which is connected to lan network. I have IP address of that two machines. I have used code like this to achieve it ,
$file = 'file.text';// inside htdocs folder
$newfile = '\\\\192.168.1.15\\htdocsfolder\\';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
but copy failed. How to debug, why file is not copied to another computer htdocs folder? whether i need to change share access on both computers?
Please help.
Thanks
You seem to be on a windows machine. Here are a couple of things to keep in mind and check for (which are a variation of the same theme):
If this is a scheduled task/batch job, try executing it from the terminal and check any errors you might get (beware of relative paths):
C:\> path-to-php script.php
Otherwise, if you can, run it as the webserver's user and see below:
Webserver process needs to have write access to \\192.168.1.15\htdocsfolder and read access to htdocs folder.
Check the user your webserver process runs as. Use your favorite process/task/service manager to check for the exact username and see if this user has the required access modes.
If you are in an Active Directory environment give access to the username identified above
If OTOH you are in a LAN, make sure the username exists (is a valid user) and has the same password on both machines.
If you do this right, permissions for Everyone are not necessary (and usually a bad thing)
Not to pick on MS and Windows, but do make sure about shares and permissions from "Manage my computer" -> "System Tools" -> "Shared Folder" as specified by Microsoft
Debugging is hard, you'll have to do a lot of further research on your own, Google and SO are your friends.
please check that the user executing php script has the access on remote file system.
If scripts are executed on Linux, its useful to mount remote share.
Just to add, your code worked fine with my case.
So I created a couple of directories and files with FTP, thus the owner is the username I use to login to the server. Now I'd like to allow users of the website to upload images to those directories. Unfortunately for the website to store images, it should be owned by Apache. How can I fix this? I've been reading around on this but can't directly find an answer.
I don't have SSH, so I guess all command-line-things are not applicable for me.
Edit
I tried to then make the folders again using apache, but now ofcourse I can't write any files using ftp into those directories.
Provided that at least the one directory is writeable by the apache user (lets call this directory 'writeabledir', it may be your root dir '/'), you must delete the folders you created via ftp and create a php script to create the directories you need.
If for example you want a directory called users and inside it another directory called upload
Create file makedirs.php on your server.
<?php
$oldumask = umask(0);
mkdir("writeabledir/users/upload",0777,true); // or even 01777 so you get the sticky bit set
umask($oldumask);
?>
Now run your makedirs.php once, by calling your.serv.er/makedirs.php on your browser
EDIT:
If you don't want to delete and recreate your directories,you could always try to change file permissions from ftp.
For exampe with FileZilla, just right click on the desired folder and set permissions to 777. If your ftp user does not have permission to do this, then there is no other way, except from asking an administrator to do this for you.
EDIT2:
Added umask to ensure that folders created by apache are writeable by everyone. (taken from http://us3.php.net/manual/en/function.mkdir.php#1207 )
Friend looks I work in php, some versions change the way of solution, however the most common is already that you want to store it would be necessary to create a database and import it to esu code that also serves to some images you want to come place, plus the wisest thing to do and you create a database with the fields necessary for its realization, import, put in a file directory of your schedule, you also advise using aptana Studio 3 greatly facilitates the creation of codes among many things and low xampp it already comes with apache integrated in one place will help you a lot any questions on installation just look at youtube he will describe
I am building a web page with Apache2.2, PHP5.2.2 on a Windows XP computer in a localhost configuration. I'm developing app/pages/submitProcessor.php to validate photo uploads from users. It validates file existance, size, mime type, drops unwanted characters, assigns a new file name, and uses move_uploaded_file() to store the file in app/uploads. I read in PHP - Question about uploading & uploaded image file that this photo storage file should be write only from app/pages/submitProcessor.php and it would be nice if it were read only from code within app/pages.
I've read a lot of info, and being pretty new to this, I still don't understand how to set read/write permissions in Windows XP in something resembling my configuration. I'm completely confused by 777, 775, 755, php.ini vs httpd.config and linux vs Windows. I'm also not comfortable with command line stuff, and would prefer to edit the appropriate file, if that is possible. How do I configure Apache so any file in app/uploads will not be executable, will write only from app/pages/submitProcessor.php, and read from app/pages/display or others in app/pages . . . or at least I'd like to get close to that. Not executable in the app/upload directory is pretty important to me.
If you are running Apache as a Service (the default setup for stand-alone & WAMP Apache installations), then that Apache Service is running under Windows' LocalSystem account.
This Windows account already has full read and write ('777') permissions on most local paths.
So when you read instructions to chmod 777 this, chmod 755 that, etc, ... you can ignore those parts of the instructions. Apache already can read-from and writeout-to those directories (unless it's a UNC path of a networked drive).
Setting File Permissions with chmod on Windows for Apache and PHP
I'm not 100% positive if that was the account on Windows XP (it is on Vista and up), but the behavior was the same.
In Windows, access to directories/folders is set by right clicking the directory, and reading through the selections provided by the various tabs to set access and specific uses of the directory. Its not as fine-grained as CHMOD, but it was good enough for my purposes at the moment.