cpanel allow write files to another user folder - php

I am trying to setup a way where i can copy or write files from one user account to another user account folder using php. Here is the example
<?php
copy('texttest.txt','/home/username/public_html/domain.com/texttest.txt');
?>
I get permission denied error. I have disabled apache mod_userdir protection. I can access the file from the folder but cannot write to the folder. I am looking for a non-cURL or Cron way. just using simply like copy() or move_uploaded_file() as this is the requirement. Anyone can help with this in setting up permissions or any suggestions?

You can do it by php FTP extension, you can see example on these reference.
How To Copy Files Around FTP Using PHP
php upload file from server to another via FTP?

Related

PHP can't open uploaded files

I have Ubuntu Server with lamp.
When i uploading files like xls or images it works fine, but when I trying to open it says me that the Excel file was broken. Same thing happens with images
Is it possible that php changes the file?
Permissions for /var/www/publick_html/uploads is 777
well, I know,I use windows and maybe this is extremely OFF TOPIC, but I have a simililar problem with uploaded files and my experience can be usefull for other people.
In my case, the uploaded file can not be open because it was written first in
C:\Windows\Temp
and then moved to the upload directory, but my temp folder doesn t have permission to access to it. Infact, if I click on temp folder I see:
After I clicked "Continue", the problem has been solved!
The problem was in versions of apache and php.
Version of apache was 2.2 and php was 5.4
I upgraded my apache to 2.4 version and now it works fine.
This should solve your problem with being unable to open uploaded files with PHP on SQL SERVER. It solved mine.
You may come across the following problem using PHP on Microsoft IIS: getting permission denied errors from the move_uploaded_file function even when all the folder permissions seem correct. I had to set the following to get it to work:
Write permissions on the the folder through the IIS management console.
Write permissions to IUSR_'server' in the folder's security settings.
Write permissions to "Domain Users" in the folder's security settings.
The third setting was required because my application itself lives in a secure folder - using authentication (either Basic or Windows Integrated) to identify the users. When the uploads happen IIS seems to be checking that these users have write access to the folder, not just whether the web server (IUSR_'server') has access.
Also, remember to set "Execute Permissions" to "None" in the IIS management console, so that people can't upload a script file and then run it. (Other checks of the uploaded file are recommended as well but 'Execute None' is a good start.)
reference https://www.php.net/manual/en/features.file-upload.php

How to download a file from ftp on server and then use import data from it

Hi I have build a script that downloads files from ftp account and then import data from them. However it works on localhost as after downloading files I save them on c drive and then read the files from there and process them. I need help to do this on live server. How can I achieve this and where can I store the files on server outside public_html folder.
I would really appreciate if someone can help me please. Thank you.
Yes, it will be good to store the files outside public_html
You can create download folder outside public_html using various tools. Log into you host provider account and use file manager tools there or else you can get an SSH credential for the host and create the folder using some SSH client
Also give proper permission to the folder, so that we can write/ read from it.
You can refer the location of the download folder from script using the script $_SERVER['DOCUMENT_ROOT'] .(Replace public_html with down load folder name)
this is not a very specific question, but sounds like you need to check permissions on your download folder on the server, set permissions so that your script can save into it, and you may want to keep it outside of public_html so that it is not accessible via the web server?
For example save it in public_html/../ftp (just illustrating the path here).

Allowing upload and browse but disallowing download

I think this question should be something easy but after searching all over the web I couldnt find an answer, so I decided to ask here.
I have a file uploader in my website that works with php. The folder where files are being uploaded has 777 chmod. I also have a php script to list the files in that folder. What I need is to allow php to upload and browse files on that folder, but dont allow people to do it. The only solution I imagined is to chown that folder to another user different than default, so I could later chmod in filezilla and allow only owner to do it, so people will see the files trough the output of the php script, but not if they navigate to that folder.
Im using Debian, apache2. Id like to know what could I do.
To make it shor, my aim: allow php to upload, read, write and execute files in that folder, but not clients unless they use my php script.
Thanks in advance
Put all the files you're talking about in their own directory. Add a .htaccess file to that directory. The contents of the .htaccess should be deny from all.
This will prevent any user from manually accessing the files as access will be blocked off. Your PHP script can still browse the contents of the file and serve it up as an attachment with the correct content type.
For more info on how to serve a file for download in PHP, read this: https://serverfault.com/questions/316814/php-serve-a-file-for-download-without-providing-the-direct-link
All services including web servers run in a security context which is an account in the OS, for example apache starts using apache user in apache group. It is enough to change mode and change owner to this user and group. Never chmod a directory to 777 until there is a good explanation for that. Using this trick, web service process only can read, write and execute in that directory.
As well, if you want the browser clients not to see(read) the contents of that directory, you should deny listing on that directory. I think it is disabled for default.

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.

Giving file write permission using php

I have a file in my project folder.How i can i give file write permission using php.I used this code
chmod($file,0777);
But it giving an error
Warning: chmod() [function.chmod]: Operation not permitted
The file is created by another user.Is their any way to do this .Thanks in advance
This happens because PHP does not have rights to do the change. The user under which PHP runs is usually the web server's user and different from the user you use to add files.
You generally only do chmod on files created with PHP. To be able to do this on other files you need to change the owner (chown).
The current user is the user under
which PHP runs. It is probably not the
same user you use for normal shell or
FTP access. The mode can be changed
only by user who owns the file on most
systems.
From http://php.net/manual/en/function.chmod.php
Well - you just can't if it says you are not permitted to.
Point is - the file belongs to some user and group, most likely root:root - and you are just a user on the server. If root's the owner of that file, you can't change the permissions at all.
Notes:
$file must be a filename. If you put the handle there, the file (most likely) doesn't exists, but still.
Check if the filename is not beginning with / or something. Try different variations.
you can install php via SUEXEC or SUPHP instead of mod_php which allows to change the user as which php is executed, still this dosnt solve anything if the owner is set wrong

Categories