0

I have a linux server running ubuntu 10.10 maverick with apache and php installed, in the /var/www directory I have a download directory. I have used OSFile manager and modified to include a function to split files here is the code:

function spl($fname){
 global $folder;
 if (!$fname == ""){
    maintop("Split");
    $dirfile = $folder.$fname;

    \\escape directory path
    $dirfile = str_replace(" ","\ ",$dirfile);
    $dirfile = str_replace("(","\(",$dirfile);
    $dirfile = str_replace(")","\)",$dirfile);
    $command = "split -d -b1024m ".$dirfile." ".$fname."_";
    echo "Command: ".$command;
    echo"<br />";


    $returnval =0;
    unset($output2);
    $output = exec($command,$output2,$returnval);

    //echo results
    echo($output);
    echo"<br />";
    print_r($output2);
    echo"<br />";
    echo($returnval);
    echo"<br />";
    mainbottom();
 }
  else
    home();
}

The command does not execute. The file and directory is correct but it returns exit code 1, and Array() as the output values, I have created a group that includes a few users, I have made this group the owner of downloads/ and given it read write and execute permissions, this group includes www-data.

Any Help will be apreciated

Jiraiya-Sama
  • 5
  • 1
  • 5
  • Tip 1: Utilize `addcslashes` or `escapeshellarg`. Tip 2: look into the `error.log` if you don't capture any error messages. – mario Oct 22 '11 at 11:10
  • where is error.log located? I have tried escapeshellarg but it did not do anything to the file path. – Jiraiya-Sama Oct 22 '11 at 11:17
  • It's a webserver file. Often above the docroot. Escapeshellarg adds single quotes around and backslashes within if needed. – mario Oct 22 '11 at 11:21
  • I read that it is suppose to escape a string that will be passed as a shell command but it did not escape the spaces or (), I have checked /var/log/apache2/error.log but it is empty – Jiraiya-Sama Oct 22 '11 at 11:25
  • It does not need further escaping if enclosed in single quotes, *which it does do*. If you don't get any log entries, or can't find them, then wrap your exec statement in `exec("sh -c \" $cmd 2>&1\"");` – mario Oct 22 '11 at 11:28
  • I have typed it exactly as you said but the exec statement still does not output anything – Jiraiya-Sama Oct 22 '11 at 11:47
  • ha! nevermind, it did give output,(I accidentally added a space before the & , I am new to vi) it says permission denied – Jiraiya-Sama Oct 22 '11 at 11:49
  • Hoora Soldier! It seems to be working, It was trying to write the file to /var/www instead of /var/www/download, I amended the code and it is now generating the parts files. Would you mind explaining what the 2>&1 does, or just providing a link that will? Also if you care for it post an answer that I may choose to be correct. – Jiraiya-Sama Oct 22 '11 at 11:56

2 Answers2

0

try this file split class

Split and merge files

or this using exec Split big files using PHP using exec

Community
  • 1
  • 1
Arun Kumar
  • 1,190
  • 8
  • 15
  • Hi, I am looking into the class you have linked, will let you know if it works. I was on that question, and I am executing the command correctly( I have tested it in a shell) but it does not work through php – Jiraiya-Sama Oct 22 '11 at 11:36
0

IF you don't get any error messages from exec and the error.log stays empty, then use this wrapper for executation:

 exec("sh -c \" $cmd 2>&1 \"");

The 2>&1 redirects the stderr channel to stdout, so that it shows up in the result array.
In the shell, what does " 2>&1 " mean?

Another option would be proc_open where you can read each channel separately, but is more work to set up.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291