Unable to chmod files: "Operation not permitted" - php

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.

Related

ZipArchive::close(): Renaming temporary file failed: Permission denied

I have this strange error, when I try to delete a file inside a compressed directory :
ZipArchive::close(): Renaming temporary file failed: Permission denied in /MyDirectory/myphpscript.php
Here is my code :
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$compressedDirectoryPath = '/Users/Shared/SampleZip.zip';
$zip = new ZipArchive();
if ($zip->open($compressedDirectoryPath) === true) {
if ($zip->deleteName('SampleZip/samplefile.txt') === true) {
echo 'File deleted';
}
}
$zip->close(); // the error is pointing here
?>
The echo executes successfully and prints File deleted. I am running a Mac and the permissions on the compressed directory is read & write for all users. What could be the issue?
As the error tells you, this is a permission problem. Make sure the apache user (www-data) has the write permission on the directory where the zip archive is.
After that, your code will work as expected.
Good luck !
This can also happen when you open the output file on server itself and keep it open while trying to run the script again.

what i can do with error : permission denied in hosting service

i registered in one of the free hosting service i have a problem with extracting files , in panel of host i haven't facility for extract files so
i just write a script in php to do this , but i got this error : permission denied ! so in your idea can i do anything (like changing permission) or it should do with Linux administrator ???
(i can just do chmode to change the access permission on files and folders)
<?php
$zip = new ZipArchive;
if ($zip->open('main.zip') === TRUE) {
$zip->extractTo('/myzip/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
The code line $zip->extractTo('/myzip/'); implies you are extracting against root directory of the file system or your assigned chroot. You might not have permissions to do so - try this instead:
$zip->extractTo('./myzip/');

php chmod() not changing permissions

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.

how to create a txtfile with 777 permission

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

What's causing this permission's error and how can I work around it?

Warning: move_uploaded_file(/home/site/public_html/wp-content/themes/mytheme/upgrader.zip) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/site/public_html/wp-content/themes/mytheme/uploader.php on line 79
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phptempfile' to '/home/site/public_html/wp-content/themes/mytheme/upgrader.zip' in /home/site/public_html/wp-content/themes/mytheme/uploader.php on line 79
There was a problem. Sorry!
Code is below for that line...
// permission settings for newly created folders
$chmod = 0755;
// Ensures that the correct file was chosen
$accepted_types = array('application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/s-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type)
{
$okay = true;
break;
}
}
$okay = strtolower($name[1]) == 'zip' ? true: false;
if(!$okay) {
die("This upgrader requires a zip file. Please make sure your file is a valid zip file with a .zip extension");
}
//mkdir($target);
$saved_file_location = $target . $filename;
//Next line is 79
if(move_uploaded_file($source, $saved_file_location)) {
openZip($saved_file_location);
} else {
die("There was a problem. Sorry!");
}
It seems that you will need to add write permissions to the folder that the zip file is being moved to. I am assuming you are using Linux and apache. You can change the owner of the upload folder to apache and give it 770 permissions. An INSECURE alternative is to not change the owner of the folder and change the permission to 777, which like I said is not secure.
The following article provides some more info in addition to some techniques to secure the second alternative I provided:
http://www.mysql-apache-php.com/fileupload-security.htm
If you have access to your server, have a look at you .htaccess file and php.ini folder to check what files are allowed to be uploaded. If you are hosting through a company, you should have access to an online control panel that has a php settings section.
Try to use the chmod function before the move script, and add write permission to that folder.

Categories