0

I have this small php code

$command = 'bash newpdftoebook.bash ' . $_POST['BookID'] . ' "' . $_POST['pdffile'] . '"';
$descriptorspec = array(
    // stdin is a pipe that the child will read from
    0 => array("pipe", "r"),
    // stdout is a pipe that the child will write to
    1 => array("pipe", "w"),
    // stderr is a pipe that the child will write to 
    2 => array("pipe", "w")
);
flush();
$process = proc_open($command, $descriptorspec, $pipes, realpath('./'), array());

And this is my bash file

dirhome=/var/www/html/
dirprocessing=$dirhome"processing/"
if [ ! -d "$dirprocessing" ]; then
  echo -e "Error: there is no folder 'processing'"
fi

pdffile=$dirprocessing"action-"$1".pdf"
if [ ! -f "$pdffile" ]; then 
  #clear dirftp
  rmdir=$dirprocessing"*"
  rm -R $rmdir
  #copy from google drive to local ubuntu server *.pdf 
  wget -O $dirprocessing"action-$1.pdf" "$2"
fi

It is not downloading the file, but when i execute it within the terminal the file is downloaded successfully. I am running this inside my terminal

bash /var/www/html/newpdftoebook.bash 2569 "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf"

What i am doing wrong, btw this code was working fine before that, but i installed a new Ubuntu 18 machine and moved the code there and since then the code dosent work anymore. Also i have installed wget on the machine as i have test it and it is working from terminal.

Dimitar
  • 1,830
  • 5
  • 32
  • 52
  • My first debug step would be to try using the full path for the `bash` exectuable. You can find the correct path by running `which bash` in the terminal – Dan Nov 21 '18 at 15:18
  • @dan08, which bash returned `/bin/bash` i tried with the full path, but without luck :( – Dimitar Nov 21 '18 at 15:30
  • Possible duplicate of [Execute root commands via PHP](https://stackoverflow.com/questions/8532304/execute-root-commands-via-php) – Mike Q Nov 21 '18 at 15:55
  • @MikeQ, my commands are not suppose to run with root privileges – Dimitar Nov 21 '18 at 17:00
  • Put a `set -x` in your bash script and look at the output. I would guess that from within the PHP script, you're somehow messing up passing the parameters correctly – darnir Nov 25 '18 at 11:04
  • @MikeQ, at the end of the day it was a permission issue – Dimitar Dec 10 '18 at 16:25

1 Answers1

0

Thanks to Mike Q I figure out why wget was not working. It turn out it was a permission issue. I just had to change the folder owner to www-data which in my case was running the apache2 server and executing the php script, because that the folder was owner by root user. To find out which user is running apache2 service (in 99% it is www-data) you can check this link or just execute this script in php

echo shell_exec('whoami');

after that just go to the main folder and just change the rights with this command in the terminal

$ sudo chown -R www-data:www-data [folder-name]
Dimitar
  • 1,830
  • 5
  • 32
  • 52