Search code examples
phpapachenginxfile-uploadfile-permissions

Generate a file by PHP


I want to generate a file by batch.php, which is linked to the button in this page:

<?php
  echo "Hello world!";
  'cat 123 > uploads/cat.txt';
  echo "Bye world!";
?> 

Clicking on the button does launch batch.php, however, cat.txt is NOT generated on the server side.

Does anyone know how to fix this? Maybe I need to setup some permission for the folder?

Edit 1:

ps aux|grep -E 'apache|www-data|http|php' returns:

root     12269  0.0  2.1 278256 10796 ?        Ss   Aug02   3:38 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
www-data 12301  0.0  0.8  97728  4044 ?        S    Aug02   3:27 nginx: worker process
www-data 12302  0.0  0.8  98104  4296 ?        S    Aug02   3:54 nginx: worker process
www-data 12303  0.0  0.9  97940  4520 ?        S    Aug02   3:46 nginx: worker process
www-data 12304  0.0  0.9  98132  4544 ?        S    Aug02   3:47 nginx: worker process
timur    14888  0.0  0.1  11712   932 pts/0    S+   15:04   0:00 grep --color=auto -E apache|www-data|http|php
www-data 18748  0.0  8.7 298196 43668 ?        S    Sep07   0:12 php-fpm: pool www                                                       
www-data 18749  0.0  6.4 286660 32432 ?        S    Sep07   0:11 php-fpm: pool www                                                       
www-data 21100  0.0  8.3 296204 42048 ?        S    Sep05   1:02 php-fpm: pool www    

ls -l returns:

total 20
-rw-r--r-- 1 root     root       84 Sep 12 08:33 batch.php
-rw-r--r-- 1 timur    timur    5120 Sep 12 09:00 generateFile.exe
-rw-rw-r-- 1 timur    timur     138 Sep 12 08:28 index.html
drwxrwxrwx 2 timur    timur    4096 Sep 12 15:03 uploads

I granted 777 to uploads, and I tried all the possible owner names and owner groups (ie, timur:timur, www-data:www-data and root:root), but none of them works...


Solution

  • The key is to write exec('cat 123 > uploads/cat.txt') or shell_exec('cat 123 > uploads/cat.txt') instead of 'cat 123 > uploads/cat.txt'.

    Then, although drwxrwxrwx 2 timur timur 4096 Sep 12 15:03 uploads works, assigning 755 and www-data:www-data (reserved to PhP) to uploads is more correct.