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

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

Related

How to correctly upload a file to a directory [duplicate]

I am trying to upload files into office network share folder, I have permission to my user account access the shared folder, I used UNC path to directly to upload files by move_upload_file function, It was not working I got this error.
Warning: move_uploaded_file(\\17.105.103.8\History\TQ_Books\1.PDF): failed to open stream: Invalid argument in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 144
after some search I mounted that shared folder on my pc, I directly used that mounted drive name to upload the file then I am getting this error
Warning: move_uploaded_file(Z:\30.PDF): failed to open stream: No such file or directory in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 437
I am not getting what's wrong with it, how to fix this issue? do I need to do some config with apache service? I was trying SMP protocol but It is very hard to understand for me.
UPDATE
$bookExt = pathinfo($_FILES['select_book']['name'], PATHINFO_EXTENSION);
$filename = ltrim($_POST['book_no'], '0') . '.' . $bookExt;
$tmpFilename = $_FILES['select_book']['tmp_name'];
$targetDir = '\\\\17.105.103.8\\History\\TQ_Books\\'; //before mount'
$targetDir = 'Z:'; //after mount
$fileTarget = $targetDir . $filename;
if (move_uploaded_file($tmpFilename, $fileTarget)) {
die("success, uploaded");
} else {
die("not uploaded");
}
After days of research, I got the solution. PHP cannot talk directly to the network share folder in this case you have to mount the network drive to your system. after if you mount the drive still it won't work, because the problem is here Apache server logged in by the local system user account, so you have to change it to your system user account through Apache service. (you can find apache service on windows services) you have to open windows service by admin then only you can change the account.
Then you can run this code on your PHP file to mount. all set (you can see your mounted drive after restart your pc)
exec("net use Z: \\\\netowrk_ip\\folder /user:{sharefolder_domine}\{sharefolder_username} {sharefolder_password} /persistent:no 2>&1", $output, $return_var);
var_dump($output);
I hope this answer helps others.

How to extract Zip archive located under different user directory in ISPConfig 3

I face the problem with extracting the .zip archive located under different user web/ directory. Let me explain: I have 2 websites both written on pure php. First website i use to manage Second. Every client on Second website has his own folder located in root with similar code but different configs. It looks like this:
Second website
...
/var/www/clients/client0/web2/web/client1/...
/var/www/clients/client0/web2/web/client2/...
...
Like you understand owner of this files is user: web2 and group: client0
And First website location:
...
/var/www/clients/client0/web1/web/...
...
Owner of this files is user: web1 and group: client0
What I'm trying to do is to extract zip archive at Second webroot directory while working on my backend on First website when create a new client.
Here is the code i tried(this works perfect on my localhost when user: www-data and group: www-data):
...
if (!file_exists('/var/www/clients/client0/web2/web/' . $_POST['storeid'])) {
$zip = new ZipArchive;
$dir = '/var/www/clients/client0/web2/web/';
$install_dir = $dir . 'backup/';
$res = $zip->open($install_dir . 'client_install.zip');
if ($res === TRUE) {
//Try to execute
$zip->extractTo($dir . $_POST['storeid'] . '/'); //var/www/clients/client0/web2/client2
$zip->close();
//Set permitions on new store folder
chmod_r($dir . $_POST['storeid'], 0755, 0644);
echo "\nExtracted successfully to " . $dir;
die();
} else {
echo "Failed to open zip: " . $dir . "client_install.zip" . " \n";
die();
}
}
...
At the time when i run this code i get message:
Failed to open zip:
/var/www/clients/client0/web2/web/backup/client_install.zip
I know it may be related to user permissions. How can solve this considering keeping same ISPConfig environment and user permissions.
Update:
Here is the error log message:
PHP Warning: ZipArchive::open(): open_basedir restriction in effect.
File(/var/www/clients/client0/web2/web/client1/backup/client_install.zip)
is not within the allowed path(s):(/var/www/clients/client0/web1/web:/var/www/clients/client0/web1/private:/var/www/clients/client0/web1/tmp:/var/www/test.site.com/web:/srv/www/test.site.com/web:/usr/share/php5:/usr/share/php:/tmp:/usr/share/phpmyadmin:/etc/phpmyadmin:/var/lib/phpmyadmin)
in /var/www/clients/client0/web1/web/process.php on line 261.
My question may sound stupid, so I'm sorry in advance. But can a change in the entry in a particular website of open_basedir option affect the performance of the entire server, or the inability of Apache to restart. My question is because my test website is located on a server with production sites. And I would not want to make these changes without initial consultation. So I think I need to add a new path to the open_basedir site parameter. People who know how this option works specifically, please respond to this post. Thanks in advance.

unable to move files to mounted network share folder

I am trying to upload files into office network share folder, I have permission to my user account access the shared folder, I used UNC path to directly to upload files by move_upload_file function, It was not working I got this error.
Warning: move_uploaded_file(\\17.105.103.8\History\TQ_Books\1.PDF): failed to open stream: Invalid argument in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 144
after some search I mounted that shared folder on my pc, I directly used that mounted drive name to upload the file then I am getting this error
Warning: move_uploaded_file(Z:\30.PDF): failed to open stream: No such file or directory in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 437
I am not getting what's wrong with it, how to fix this issue? do I need to do some config with apache service? I was trying SMP protocol but It is very hard to understand for me.
UPDATE
$bookExt = pathinfo($_FILES['select_book']['name'], PATHINFO_EXTENSION);
$filename = ltrim($_POST['book_no'], '0') . '.' . $bookExt;
$tmpFilename = $_FILES['select_book']['tmp_name'];
$targetDir = '\\\\17.105.103.8\\History\\TQ_Books\\'; //before mount'
$targetDir = 'Z:'; //after mount
$fileTarget = $targetDir . $filename;
if (move_uploaded_file($tmpFilename, $fileTarget)) {
die("success, uploaded");
} else {
die("not uploaded");
}
After days of research, I got the solution. PHP cannot talk directly to the network share folder in this case you have to mount the network drive to your system. after if you mount the drive still it won't work, because the problem is here Apache server logged in by the local system user account, so you have to change it to your system user account through Apache service. (you can find apache service on windows services) you have to open windows service by admin then only you can change the account.
Then you can run this code on your PHP file to mount. all set (you can see your mounted drive after restart your pc)
exec("net use Z: \\\\netowrk_ip\\folder /user:{sharefolder_domine}\{sharefolder_username} {sharefolder_password} /persistent:no 2>&1", $output, $return_var);
var_dump($output);
I hope this answer helps others.

PHP is_writable false for NFS folder although files can be written

Sorry, I'm not sure, if this is the correct forum because I don't know the cause for the issue, I'm facing.
I installed NextCloud on a Raspbian (Stretch 9) and moved the data directory to a mounted NFS folder. When I try to access NextCloud, I got the error message 'Data directory is not writable'.
So I dug a better deeper and could finally isolate the issue to the interaction between PHP7.0 and the NFS:
For some reason, the application can write to the directory but is_writable returns false.
I have created the following PHP script:
<?php
$dirname = '/var/churros/data/nextcloud/';
//$dirname = '/tmp/';
$myfile = fopen($dirname.'newfile.txt', "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
echo nl2br("File ".$dirname."newfile.txt written\n");
if (touch($dirname.'/chkpt.tmp')) {
echo nl2br("touch(".$dirname."/chkpt.tmp) successful\n");
} else {
echo nl2br("touch(".$dirname."/chkpt.tmp) failed\n");
}
if (is_writable($dirname)) {
echo 'Directory '.$dirname.' is writable';
} else {
echo 'Directory '.$dirname.' is not writable';
}
phpinfo();
?>
The result is that
newfile.txt is created in the data directory with the given text (John Doe)
Touch succeeded, i.e. the checkpoint file is created
is_writable returns false Screenshot of 'debug.php' with NFS directory
When I change to directory to a local directory like \tmp everything is fine Screenshot of 'debug.php' with /tmp directory
My NFS is mounted as
192.168.1.100:/volume1/pidata/donut on /var/churros type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.103,local_lock=none,addr=192.168.1.100)
and obviously the user mapping and access rights are correct:
namei -l /var/churros/web/nextcloud/
f: /var/churros/web/nextcloud/
drwxr-xr-x root root /
drwxr-xr-x root root var
drwxr-xr-x root root churros
drwxr-xr-x www-data www-data web
drwxrwxr-x www-data www-data nextcloud
On the command line, as user www-data, I can access the directory and write to it as well.
Finally, SELinux is not installed/enabled on the box.
So: Any idea why PHP is_writable fails on the NFS directory or how I can debug this PHP function?
The issue could be the unix user id is different for user "www-data" for the 2 different systems.
In detail, from the php src, you can see that:
is_writable() is defined at ext/standard/php_filestat.h, which uses:
php_stat header file defined at php_stat()
VCWD_ACCESS() function is used
In turn:
VCWD_ACCESS()
is a convenience wrapper for virtual_access()
virtual_access()
is thread safe wrapper around the access() function that takes
per-thread virtual working directories into account.
Finally, looking access() doc:
The access() function checks the file named by the pathname pointed to by the path argument for accessibility according to the bit pattern contained in amode, using the real user ID in place of the effective user ID and the real group ID in place of the effective group ID.
and also at the access() linux documentation, it states:
access() may not work correctly on NFS file systems with UID mapping enabled, because UID mapping is done on the server and hidden from the client, which checks permissions. Similar problems can occur to FUSE mounts.
Try:
var_dump(stat('nfs-filename'));
and see which uid you get.
Reference:
Similar issue with php session in nfs
Most likely this is is_writable() function's bug.
You may fix this NextCloud issue:
} else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
//common hint for all file permissions error messages
$permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.',
[$urlGenerator->linkToDocs('admin-dir_permissions')]);
$errors[] = [
'error' => 'Your data directory is not writable',
'hint' => $permissionsHint
];
AND
/usr/share/webapps/nextcloud/lib/private/Console/Application.php
if ($input->getFirstArgument() !== 'check') {
$errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
if (!empty($errors)) {
foreach ($errors as $error) {
$output->writeln((string)$error['error']);
$output->writeln((string)$error['hint']);
$output->writeln('');
}
throw new \Exception("Environment not properly prepared.");
}
}
Source #1, Source #2

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.

Categories