PHP can't move (write) uploaded files to mounted network drive - php

We develop our PHP-based web applications by editing our working files in a shared, local directory on each of our windows machines. The (linux) staging server then mounts our shared drives and serves them each under subdomains. E.g. joe.work.com would serve Joe's working directory. We then access our own staging sites by editing our hosts file to point the subdomain to the staging server...and all of that works great!
We're now running into the issue that PHP doesn't seem to have permission to move uploaded files from the tmp directory to a directory inside of the mounted directory (which is actually the shared windows drive...?).
The working directory on the windows machines are set to allow everyone rw, and I have tried putting 777 on the mounted directory in the staging machine, but I am still get permission denied.
The shared drive, say, \\joes_machine\joes_working_dir mounts on the staging server to /var/mnt/joe. The file upload needs to be moved to /images/common.
Albeit slightly dumbed down for this example, I'm not doing anything fancy code-wise:
$working_directory = '/var/mnt/joe';
$image_directory = '/images/common/';
$full_filename = $working_directory . $image_directory . $filename;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $full_filename))
// do some other stuff
My error of course is:
Message: move_uploaded_file(/var/mnt/joe/images/common/resulting_filename.jpg):
failed to open stream: Permission denied
Message: move_uploaded_file(): Unable to move '/tmp/phpjsEfBc' to
'/var/mnt/joe/images/common/resulting_filename.jpg'
What am I not understanding about file permissions pertaining to a windows shared drive being mounted over the network by linux and PHP needing to write to it? I can't seem to find the hang up!
Once we hit production, we won't be using the schema, but if there's a simple solution to be able to continue in our current development environment, then that would be ideal!

After a few hours of facerolling, I finally found what I was missing. The issue was that the credentials supplied couldn't write to the mounted directory as expected. The way I was able to fix this was by editing the mount command as follows:
mount -t cifs //shared/directory /mount/target
-o rw,username=connectionuser,password=password,uid=48
so, username and password are to be the windows credentials used to connect to the drive, but uid specifies the unique identifier of the local user on the staging server that apache runs as so that it may write to the mounted directory.
previously, i had not specified the uid of the local user, so when apache was trying to write to the mounted directory, it was trying to use the windows credentials (that couldn't write on the 'local' drive)
hope this is helpful!

Related

Can't write to a remote server location with PHP but I can browse and write to it in File Explorer?

Sorry if this a repeat of a question that has been asked before but I have not been able to find my exact situation. We are trying to migrate our website server from a Windows 10 VM (yes I know) to a Windows Server 2019 VM. We have some PHP on our site that writes files to some of our other servers on the same domain and have been able to do so without issue using file_put_contents like so:
file_put_contents("\\\\server\\folder\\folder\\folder\\".$filename, $file);
Now all of a sudden, to run the same code on our new server I get a Warning on this line, "Failed to open stream: Permission denied". I have permission to access this folder, I can browse to \server\folder\folder\folder and create a file there. I even tried mapping this server to a letter drive on my new web server, and still same error. I can put the file on the local C drive just fine but that's it.
Running fileperms on the folder path gives Warning: fileperms(): stat failed. Running is_writable on the folder path returns false, I just can't see how. Running it on the old Windows 10 web "server" returns true. I've read some things about needing to enable certain settings on the server you're trying to access, but I just can't think of what would allow one VM to access it and not another. Both VM's are logged in with the same user with admin rights. I can bring up the same folder in file explorer and write to it, just not via PHP. What obvious thing am I missing?
Thanks!
After weeks of banging our heads against the wall we finally figured this out. On our old web server, when right-clicking the website from the Sites file tree in IIS, under Manage Website -> Advanced Settings, the Physical Path Credentials field was set to the credentials needed to access these folders. On the new server it was blank. I'm not sure how this got missed but in any case, after entering the correct credentials here everything immediately worked.

Can't view re-uploaded php web files in my Linux server

I used to have a Windows OS server where i uploaded some old php web files to it. I could then access them, edit them, and view them online via my host name.
After much debating and reasoning, we had to change the OS of the server from Windows to Linux. After the change had been completed, a backup of the server was uploaded to the new Linux installation where all my old files were kept.
I could view these files online as I used to do when the server had windows OS.
The only thing I did encounter was the following:
a) I downloaded my files from the server using putty,
b) I deleted the old copy in my Linux server,
c) I then re-uploaded the same file that used to be in the server without making absolutely no change whatsoever to it, to the exact place where it was,
d) When I try to access it via its web address like I did earlier, it throws an error message saying..."The page isn't working".
I don't know much about Linux and there fore I am stuck. I don't know what the problem is. I can't understand why I can view all the files via their web address if they were placed there from the backup, but when I download them, delete their file from the server and then re-upload the exact same downloaded file to the exact place where it used to work, I get an error message.
Extra info: I connect to this Linux server from a windows OS machine using putty.
I found the problem. Since I migrated from a Windows OS server to a Linux Cent OS server, I didn't know that you had to configure the privileges of each folder in order to be accessed from the web. By default, my uploaded files where tagged by ownership of "user". The server was configured to only display files that were tagged by ownership of "root". The way I solved this was by typing the following command in the terminal.
NOTE: "You have to be in the folder where the file you are going to change ownership is."
sudo chown root:root filename.php
sudo -> Execute in admin mode
chown -> Change ownership of file to...
root:root -> ... root instead of user
filename.php -> the name of my file
Executing this corrected the error. Hope it helps someone else since I coudn't find anything related.

Curlftpfs mount can't reach files via apache webserver

I have mounted a directory via curlftpfs. I am trying to access the files from within this mount via a webserver.
The path to the mounted directory is
'/home/domain.nl/domains/cms.domain.nl/public_html/project/1/syslink/'
The strange thing is that PHP can read and write from/to that directory. But when i try to access the folder from my apache webserver, then i get the error:
403 Forbidden
You don't have permission to access /project/1/syslink/file.jpg on
this server.
The output when I run exec('whoami') in php is:
urre
That is also the user that created the curlftpfs mount, so the rights are not the issue.
When I log in to the linux CLI with this user, I can access the mounted directory...
Apache Webserver requires the user "www-data" to be able to read and write to a folder / document.
You must change, either your FTP mount user to "www-data" or change your Apache running user.
In /etc/fstab
curlftpfs#user:pass#domain/directory fuse auto,user,uid=www-data,allow_other,_netdev 0 0
If you would like to set the group as well, you can add "gid=www-data" to fstab
This should allow Apache to read and write to your mounted FTP directory.

How to set read/write permissions in Apache on a Windows computer

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.

Permission Denied on move_uploaded_file to another server in IIS

I have a PHP web application running on IIS, which allows a user to upload a file from a simple html form. The uploaded file is then moved to a folder on another server. This is working correctly on Apache, but fails on IIS with the error:
function['move_uploaded_file']failed to open stream: Permission denied
in...
I've checked all the permissions on the directories. If I run a simple PHP script through the command line to copy a file from the server into the folder on the other server it works correctly, so I suspect the problem is with IIS.
if (move_uploaded_file($_FILES ["file"] ["tmp_name"], "\\\\000.00.0.00\\tia\\web\\upload\\" .$_FILES["file"]["name"])) {
This has been covered already here. Quoting the reason here:
You have mapped target directory to a share such as \server2\files. These mappings are only available to the current users (eg, the admin account), not to the IUSR account that your php is probably running as (assuming IIS). Solution, don't use mappings, instead use the full 'unc' path name, ie '\server\share\folder\file.ext', also remember that the IUSR account will need access to these shares/folders/files
From what I can see in your comment, you are using a \\ prefixed network share as the target for your move_uploaded_file() operation.
I'll bet a beer that PHP isn't allowed to access that share. Remember, PHP runs with IIS's permissions!
Try saving to a local, globally writable path first. If that works, you know you have to fix the share's permissions.

Categories