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

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.

Related

Cannot write a file to AWS EC2 instance - PHP

I am trying to create and write a file to ec2 instance. I am getting the error below despite the folder that i am writing possessing all the permissions required.
$handle = fopen("thumbnailFolder/testFile.txt", "r"); // line 4
if($handle != false){
echo 'File created!';
}else{
echo 'File create failed!';
}
The 'thumbnailFolder' has the following permissions:
drwxrwxrwx
Error message:
fopen(thumbnailFolder/testFile.txt): failed to open stream: No such file or directory in /var/www/html/book_aws/my_server/folder/web/thumbnailTest.php on line 4
File create failed!
As the error clearly say. System is failing to open the file which is not there.
$handle = fopen("thumbnailFolder/testFile.txt", "r");
Above code open files in read mode. if there is no files then throws an error.
If you want to open file to write then use try below code, this tries to open file and sets pointer at the begining if file is not there then creates the file in given name. for read and write use w+ instead of w
$handle = fopen("thumbnailFolder/testFile.txt", "w");
There are different modes with respect files. you can check below link for further details.
http://php.net/manual/en/function.fopen.php

failed to open stream, permission denied even tho permissions are set

I'm working with the bing-ads api and try to download a report to a local folder.
I already found out the user that is executing php using
<?php echo exec('whoami'); ?>
which resulted in "dl-dominikl-pc\dolo"
I gave that user full control of the "reports" folder.
The code-part that is failing is:
fopen($downloadPath, 'wb');
with $downloadPath being "C:\\xampp\\htdocs\\crm\\bingAds\\examples\\reports"
Thanks to Saitama for reminiding me to check my path which didn't point to a file!

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/');

I have A PassKit PHP Script And The rename() And mkdir() wont work. What Am I Doing Wrong?

I have a php system that will sign and zip a passbook pass. I need it to check for the file thumbnail.png and if it does not exist to make a directory with the variable $idnumber and then rename the file and move it over into the new folder. This Is My Code:
...
if (!file_exists('images/'.$idnumber.'/thumbnail.png')) {
mkdir("images/$idnumber", 0777);
rename("images/$idnumber.png", "images/$idnumber/thumbnail.png")
}
$pass->addFile('images/icon.png');
$pass->addFile('images/icon#2x.png');
$pass->addFile('images/logo.png');
$pass->addFile('images/'.$idnumber.'.png');
if($pass->checkError($error) == true) {
exit('An error occured: '.$error);
}
...
I Am Running This Code On A XAMPP LocalServer, but it is not creating the directory or renaming file.
What Am I Doing Wrong?

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

Categories