Mac OS X permissions to create a zip file - php

After searching the internet find an answer I've decided to ask for help directly.
I am running mac os x 10.7, PHP, Apache and MySQL. I am trying to let a user select files from a form have them zipped and sent to the individual. At first PHP couldn't even create a file. I found the following:
sudo chmod -R +a "_www allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" ~/Sites
Which allowed me to create a file. But when I try to create a zip I get some unknown extension being appended at the end and 2 files are being created. So not knowing what was wrong I thought I would just find the file by opening the directory and forcibly renaming it back to *.zip. I have checked the file after renaming manually and it seems to be fine.
I get the following error:
Warning: rename(tmp/RosenData082112-1102.zip.ohoKRw,RosenData082112-1102.zip) [function.rename]: Permission denied in /Users/mmcri/Sites/rosenlab/download.php on line 42
ls -le reveals this to the files:
-rw-------+ 1 _www staff 43775 Aug 21 11:02 RosenData082112-1102.zip.1qJUE4
0: user:_www inherited allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity
-rw-------+ 1 _www staff 43775 Aug 21 11:02 RosenData082112-1102.zip.ohoKRw
0: user:_www inherited allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity
Here is the PHP code section making the zip file:
if(extension_loaded('zip'))
{
$zip = new ZipArchive();
$zipname = "RosenData".date("mdy-Hi").".zip";
if($zip->open($direct.$zipname, ZIPARCHIVE::CREATE) === TRUE)
{
foreach($filelist as $file)
$zip->addFile($path.$file,$file) or die ("Error adding file: $file");
$zip->close();
}
// Random stuff below
}
Any help is greatly appreciated.
Thanks.

Do you have permissions to write on that folder in Mac?
Try with fopen instead of ZipArchive.

Related

Strange behaviour when try to extract a zip file in docker container with laravel

I'm trying to extract a zip archive in a docker container app running Laravel 9 on PHP 8.1.7, and i'm facing a weird error.
So if a try this code in a controller
$zip = new ZipArchive();
$result = $zip->open("/var/www/html/public/my_archive.zip");
if ($result === TRUE) {
$zip->extractTo("/var/www/html/public/my_folder");
}
$zip->close();
the files in archive are correctly extracted, but return this error:
ErrorException
ZipArchive::extractTo(/var/www/html/public/my_folder/my_file.xml): Operation failed: Operation not permitted
If I run the same code in php artisan tinker it works.
Anyone have some idea to fix this problem?
It don't seem a permissions related problem, the folder is created with 777 permission and the files are copied correctly.
EDIT
root#5899a5badc45:/var/www/html/public/my_folder# ls -lhart *
-rwxrwxrwx 1 1000 1000 1.3K Oct 25 12:24 phpunit.xml
Thanks

move_uploaded_file returns false but $_FILES error is 0

I'm trying to deploy a PHP project built with CodeIgniter framework on a Linux machine.
The moving uploaded file from the temp to the specified directory is not working for some reason.
Info
PHP 7.2.19
CentOS 7
CodeIgniter 3.0.6
Notes
I have checked the target containing directory exists (no duplicate files existing with same name as the to-be-moved file).
I have checked the permissions of the directory and it should be fine (I set the group to apache with permissions = owner user)
Checked the ACL and it's consistent with the ordinary permissions.
$_FILES['file']['error'] = 0 (before and after calling move_uploaded_file)
file_exists returns true for $_FILES['file']['tmp_name']
is_uploaded_file returns true for $_FILES['file']['tmp_name']
open_basedir is not set
The uploaded file is less than post and file upload limits.
The last thing to mention, brace yourself, is that there is another copy of the site exist (deployed on the same server with a different directory name) and the upload is working just fine. Diff'ing both directories to catch the different but found nothing (apart from some views with some updates, nothing related to configurations, .htacess whatsoever).
Any clue as what should I look at?
Update 1
Code:
$createdFileName = NULL;
$files = $_FILES;
if (count($files) > 0) {
$createdFileName = $this->GUID();
$target_file = './upload/' . $createdFileName;
$path_info = pathinfo($files["file"]["name"]);
if (isset($path_info['extension'])) {
$ext = $path_info['extension'];
} else /*if (!$ext)*/ {
$ext = substr($files['file']['type'], strrpos($files['file']['type'], '/') + 1);
}
$success = move_uploaded_file($files["file"]["tmp_name"], $target_file);
if (!$success) {
$result = json_encode(array('result' => FALSE, 'message' => 'Couldn\'t upload image file'));
}
}
This is the code that is working with the other project and not working with the project I'm working on.
I learned about the values of error and others from remotely debugging the project.
Update 2
The following is the permissions of the upload directory.
drwxrwxr-x. 2 demoroot apache 4.0K Jun 9 14:06 upload/
The following is the output of getfacl:
# file: upload/
# owner: demoroot
# group: apache
user::rwx
group::rwx
other::r-x
The solution is running the following command
chcon -R --type httpd_sys_rw_content_t /path/to/upload/directory
I totally have no idea what this does and why do I have to run it when getfacl output seems okay without running the command.

Why does switching to secure folder (httpsdocs) break the PHP file upload function

Server version: Apache/2.4.34 (Unix)
NOTE: This is not a Plesk or control panel questions. This is a custom PHP application hosted on a Rackspace dedicated server with no control panel.
I'm trying to utilize an existing SSL certificate for several sub-directories of a domain, but the architecture for the domain separates out two directories for non-secure/secure:
httpdocs
httpsdocs
I copied everything from httpdocs to httpsdocs and also forced traffic to https using .htaccess. Everything worked great until I tried the PHP file upload functionality.
Here's the bit of code that is failing from the secure side:
$path = "uploaded_files/".$row_query[0].'/'.$file_name;
copy($temp_file, $path);
In doing some debugging, I found that the temp file gets created:
/tmp/phpanKT4N
but the error I see in the logs is:
copy(uploaded_files/New Sub Folder/30052_testing.txt): failed to open stream: Permission denied in /var/www/vhosts/.com/httpsdocs/demo/resource/add_resource.php on line 108
I tried changing copy() to move_uploaded_file() but here's the error for that:
PHP Warning: move_uploaded_file(uploaded_files/test/30054_testing.txt): failed to open stream: No such file or directory in /var/www/vhosts/<thedomain>.com/httpsdocs/demo/resource/add_resource.php on line 111
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpOC9YEF' to 'uploaded_files/test/30054_testing.txt' in /var/www/vhosts/<thedomain>.com/httpsdocs/demo/resource/add_resource.php on line 111
I searched stack and saw that ownership must match, but the permissions appear to be the same for httpdocs and httpsdocs.
The /tmp folder is owned by root:root and like I said this works perfectly from the non-secure folder. The non-secure and secure upload folders are identical:
httpdocs:
drwxrwxr-x+ 8 theuser apache 4096 Sep 22 2015 uploaded_files
httpsdocs:
drwxrwxr-x+ 8 theuser apache 4096 Sep 22 2015 uploaded_files
Is this a configuration, permissions issue, or what?
Figured this out... Ran the following:
First find apache's user:
egrep -i '^user|^group' /etc/httpd/conf/httpd.conf
Result:
User apache
Group apache
Then ran this and it solved all the PHP file upload issues in httpsdocs:
chown -R apache:apache /var/www/vhosts/<thedomain>.com/httpsdocs/
chmod -R g+rw /var/www/vhosts/<thedomain>.com/httpsdocs/

PHP isn't able to read file

I'm stuck trying to open a file with fopen in php.
$db_ausgaenge = "statuseing.php";
$dout = fopen($db_ausgaenge, "x+");
print $dout;
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
Warning: fopen(statuseing.php): failed to open stream: File exists in /var/www/html/zufallsgenerator.php on line 33
I checked following items:
chmod for statuseing.php 0777
owner is www-data with groud www-data
script is running as user www-data
groups are uid=33(www-data) gid=33(www-data) groups=33(www-data)
pwd is /var/www/html as expected
the path the scripts want's to open is correct
checked openbase dir in php.ini showed in phpinfo(), added /var/www/html, but php doesn't care about it.
open_basedir = /var/www/html/
After daemon-reload and restarting apache2 via systemctl nothing changed, phpinfo() didn't show the path given in the config. restarting the system via init 6 didn't took effect, too.
statuseing.php already exists.
See the manual (http://php.net/manual/en/function.fopen.php) - opening in x or x+ mode says: Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE
Look at the mode you are using.
x+ means that if the file already exists an error will be thrown.
To find the correct mode depending on your scenario check out http://php.net/manual/en/function.fopen.php
Try this:
$db_ausgaenge = __DIR__."/statuseing.php";
$dout = fopen($db_ausgaenge, "a+"); // x+ will throw error cuz it tries to open existing file, thx to: bluegman991 (;
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
or if You want to truncate file before adding data use w+:
$db_ausgaenge = __DIR__."/statuseing.php";
$dout = fopen($db_ausgaenge, "w+");
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
also do some checkups:
1) check free space: df -h
2) check if You can edit that file: nano /var/www/html/statuseing.php

Server migration troubles with Imagick, Fopen, PHP and Permissions

I've recently migrated linux servers.
I'm currently getting this error when trying to upload an image using Imagick from a form.
Warning: fopen(/home/user/public/website.com/public/images/users/current/thumb/username.jpg): failed to open stream: Permission denied in /home/user/public/website.com/public/inc/functions/functions.php on line 864
The code that is run resides in a function:
try{
$im = new Imagick($image['tmp_name']);
if($im_w != 'O' && $im_h != 'O'){
$im->cropThumbnailImage($im_w,$im_h);
}
$fp = fopen($path, 'w'); //the line that is causing the error to occur. line 864.
$im->writeImageFile($fp);
return 1;
}
catch(Exception $e)
{
echo $e->getMessage();
}
I've had a look through various PHP/Linux fixes for permissions, The /images/ directory and subdirectories currently have permission: 755
Can anyone help me with a solution. Comment if you need to know anything else.
Thanks in advance.
Simply entered the command into my linux command line:
sudo chmod -R 777 /home/username/public/website.com/public/x_images/

Categories