I am having problems with a picture uploading script.
I know there are hundreds of the same questions, but I haven't found the one that would be work for me.
$upload_dir = "images/postcards/";
chmod($upload_dir, 777);
if (is_writable($upload_dir)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
This always returns that the file is "not writable"
I tried setting chmod to 0777 and -rwxrwxrwx. But result was always the same. Any Ideas?
The directory must be owned by the user invoking the script (typically www-data, apache or httpd if you're running the script in a apache/*NIX setup). A user can't set 777 permissions on directories it doesn't own.
See the note on the chmod() manual:
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.
First , open PHP error_report by adding two line on top of your code, see if there is a error coming from chmod:
ini_set('display_errors', true);
error_reporting(E_ALL);
Make sure your WebServer has the permission to that directory, my guess is the WebServer don't have permission.
I was having similar troubles using chmod, although the file was owned by apache:apache (webserver user). In my case SELinux was getting in the way, disabling it made this clear:
sudo setenforce 0
And the chmod works. Now on to figuring out how to make a SELinux exception for this case...
(and don't forget to enable SELinux, of course)
I already had the same problem you can change the file's permission by this code :
<?php
$ftp_details['ftp_user_name'] = 'your ftp username';
$ftp_details['ftp_user_pass'] = 'your ftp password';
$ftp_details['ftp_root'] = '/public_html/';
$ftp_details['ftp_server'] = 'ftp' . $_SERVER['HTTP_HOST'];
function ftp_chmod($path, $mod, $ftp_details) {
extract($ftp_details);
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to chmod $path directory
if (ftp_site($conn_id, 'CHMOD ' . $mod . ' ' . $ftp_root . $path) !== false) {
$success = true;
}
else {
$success = false;
}
ftp_close($conn_id);
return $success;
}
?>
I didn't run this code but I think it's Ok and it will help you.
tell me if your problems resolved.
Related
I am trying to run a script which creates pages and saves them to the server
but am getting a permission error on one of the files that is in the public_html directory.
So 2 pages are created in the "pages" directory which is chmod to 0777 and they are created fine.
The 3rd page is created in the "public_html" directory which fails with you do not have permission.
The only way i have found to fix this is to chmod the "public_html" directory to 0770 which then
everything works but i have been strongly advised by the hosting company not to do this bevause of the security risk.
So my question is, Is there any otherway to achieve this goal?
Looking into it a bit it looks like i need to give the script "user" priviliges might work but
this is beyond my knowledge at the moment.
I`m not even sure what the script is running as at the moment, I would guess "group" as chmoding the public_html
to 0770 allows "group"
My setup is: vps server running centos CENTOS 6.7 x86_64
php 5, dso, Apache suEXEC on
simplified Code i am using is:
$page_path = "/home/username/public_html/";
$loop[Html_Name] = "test.html";
$new_page_file = "test.html";
$fp = fopen($page_path.$loop[Html_Name], "w");
fwrite($fp, $new_page_file);
fclose($fp);
chmod($page_path.$loop[Html_Name], 0666);
Many thanks in advance.
Typically, we use ftp in these situations. /public_html permissions may remain to 750 and run this code.
$server = 'localhost';
$ftp_user_name = 'username';
$ftp_user_pass = 'passw';
$dest = 'public_html/new.file';
$source = '/home/username/public_html/path/to/existing.file';
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$connection || !$login) { die('Ftp not connected.'); }
$copied = ftp_put($connection, $dest, $source, FTP_BINARY);
if ($copied) {
echo 'File copied';
} else {
echo 'Copy failed!';
}
ftp_close($connection);
The page with final destination in public_html can be created in the other directory and then this script will copy it in public_html. The old file will remain and if a file exists with the same destination name will be overwritten.
The $dest is relative path to user home directory. The $source is absolute path.
The connection will fail if the ftp is concurrently used by filezilla or something. A solution to that is to create a second ftp user account in cPanel.
i want to create a .txt file with the 777 permission for file creation i am using the following code
if(file_exists($myFile) == true)
{
$err = "File Already Exist in The usrlogactity. Try Another Username";
}
else
{
$fh = fopen($myFile, 'w') or die("can't open file");
}
$file = "usrlogactity/$myFile";
$ftp_server="02.79.103.130";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, "use1234", "pass123");
// try to chmod $file to 644
if (ftp_chmod($conn_id, 0777, $file) !== false)
{
//echo "$file chmoded successfully to 777\n";
}
else
{
//echo "could not chmod $file\n";
}
// close the connection
ftp_close($conn_id);
if i execute this code in server it create the file with the 644 permission ,
but gives error. how to i create the .txt file with the 777 permissiom please guide me
The error is
No such file or directory in usrlogactity/$myFile on line 13
To change local file permissions you have to use PHP function chmod(), not using ftp for this
Doing it via FTP you're trying to access this file with ftp user, not www user, and, therefore, no success.
And, you see, how it's important to post your code, not to tell about it? ;-)
it depends on the server if your scripts are allowed to and why do you need 777 for a text file you don't execute a text file you read its 644 is all you need,
but permissions should not be changed by a script different configurations leads to completely different results,
Some servers are idiots and run Apache as root, which mean php writes a file that root owns,
Some use suExec and then your login to your account owns the file
others use Apache user to control your file
so your script could work on one and not on another if you moved the site due to the file becoming unreadable
Allso check safemode on your php install
i want to delete files from a directory via php.
Somehow my php_errorlog always tells me:
[06-Jun-2010 19:38:46] PHP Warning: chmod() [function.chmod]: Operation not permitted in /Users/myname/htdocs/
if ($_POST) {
echo "yeah!!!";
print count($_POST['deletefiles']);
chmod($path, 0777); //server rights
foreach ($_POST['deletefiles'] as $value) {
print $value;
unlink($path .'/' . $value);
}
//chmod($path, 0666); //server rights
}
what am I doing wrong? Thank you
Does the user PHP is running as have write access to the files you're attempting to chmod? Remember that this user is most likely not the same as your own account.
chmod these files to 0666 using your FTP client first.
move_uploaded_file() won't work for me anymore, it was working fine and just stopped out of nowhere. Is there a way for me to check why it's not working anymore? Here's what I currently have, but it only returns TRUE or FALSE.
$status = move_uploaded_file($tempFile, $targetFile);
if($status) {
echo 'its good';
} else {
echo 'it failed';
}
I know the path is 100% correct and the directory is CHMOD 755. Is there anything I might be doing wrong?
Maybe this will work:
if(!move_uploaded_file($_FILES['attachement']['tmp_name'], $uploadfile)) {
echo 'Your file was not uploaded please try again
here are your debug informations:
'.print_r($_FILES);
} else {
echo 'image succesfully uploaded!';
}
Check your error reporting level (see error_reporting function). You should get a warning or notice that's a bit more descriptive.
Also, check that the user your PHP script runs as (usually the server's user, which is nobody or www-data on a lot of systems, but YMMV) owns the directory. With 755, only the owner of the directory can write to it.
Permissions of 755 means that only the owner of the directory can write to that directory.
So the question is, who is the owner and as what user is the web-server / php running?
If they donĀ“t match, you can either change the ownership or the group (also changing the permissions to 775).
I have got the following problem since the server has safe mode turned on, and directories are being created under different users:
I upload my script to the server, it shows as belonging to 'user1'. All it is doing is making a new directory when a new user is created so it can store files in it.
New directory is created, but it belongs to 'apache' user.
'user1' and 'apache' are different users; and safe mode is turned on. So the php script cannot write to that newly created directory.
Now I have a problem!
One solution is to turn off safe mode. Also, a coworker suggested that there are settings that can be changed to ensure the directories are under the same user as the script. So I am looking to see if latter can be done.
But I have to ask. Is there a programatical solution for my problem?
I am leaning to a 'no', as safe mode was implemented to solve it at the php level. Also the actual problem may seem like the directory being created under a different user, so a programatic fix might just be a band-aid fix.
I've used this workaround:
instead of php mkdir you can create directories by FTP with proper rights.
function FtpMkdir($path, $newDir) {
$path = 'mainwebsite_html/'.$path;
$server='ftp.myserver.com'; // ftp server
$connection = ftp_connect($server); // connection
// login to ftp server
$user = "user#myserver.com";
$pass = "password";
$result = ftp_login($connection, $user, $pass);
// check if connection was made
if ((!$connection) || (!$result)) {
return false;
exit();
} else {
ftp_chdir($connection, $path); // go to destination dir
if(ftp_mkdir($connection, $newDir)) { // create directory
ftp_site($connection, "CHMOD 777 $newDir") or die("FTP SITE CMD failed.");
return $newDir;
} else {
return false;
}
ftp_close($connection); // close connection
}
}
You might be able to turn safe mode off for a specific directory via a .htaccess file (if on Apache).
php_value safe_mode = Off
You might need to get your hosting provider to make this change for you though in the httpd.conf.
I have had some success with setting the group bit of the upload directory to sticky.
PHP can then create directories inside it and write to it.
http://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories
chmod g+s directory