4

I am trying to convert a pdf file into text file. When I run the command through terminal its working fine but when try to execute it through PHP it's not working.

I am stuck in this situation from last four hour spend lots of time in google but no solution available. Can any body resolve this issue?

File owner - nobody

shell_exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf');

Can anyone provide any helpful solution?

I also tried to change usr folder ownership from root to nobody and provide 777 permission on folder and its context.

23
  • 3
    when you do shell_exec from PHP file you're under apache user, maybe apache has no permission in /usr/bin/pdftotext? Jan 25, 2013 at 12:37
  • if you run it through exec and view the $output, what do you get? Should look something like: exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf',$output = array()); var_dump($output);
    – Prisoner
    Jan 25, 2013 at 12:37
  • NULL returns after execution Jan 25, 2013 at 12:42
  • Maybe is permission denied.
    – Kai
    Jan 25, 2013 at 12:43
  • you could chown apache /usr/bin/pdftotext/ But before this, stop apache, server httpd stop then 'su apache` and check the command Jan 25, 2013 at 12:44

5 Answers 5

5
+50

Your command to run pdftotext is not correct.

There should be a second argument telling pdftotext to write to a specific file or just use a dash "-" to write to stdout, unless you actually want the program to create a text-file with the filename as the pdf (this would require write permissions in the /opt/lampp/.../ folder)

This is at least true for pdftotext version 0.12.4

"Pdftotext reads the PDF file, PDF-file, and writes a text file, text-file. If text-file is not specified, pdftotext converts file.pdf to file.txt. If text-file is ´-', the text is sent to stdout."

So, the solution to your question would simply be add a dash after the filename, like so:

<?php
$pdftext = shell_exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf -');
echo $pdftext;

Provided that the binary exists and PHP is allowed to use shell_exec and you have permissions and that the pdf-file exists and you have permissions.

2

from how to test if PHP system() function is allowed? and not turned off for security reasons

function isAvailable($func) {
    if (ini_get('safe_mode')) return false;
    $disabled = ini_get('disable_functions');
    if ($disabled) {
        $disabled = explode(',', $disabled);
        $disabled = array_map('trim', $disabled);
        return !in_array($func, $disabled);
    }
    return true;
}

You may need to check if isAvailable('shell_exec') On shared hosting this function might be disabled.

If it's not disabled, check the Apache log, it's all you can do.

2
  • he has access to terminal, i don't think it's shared hosting Jan 25, 2013 at 12:43
  • 3
    For my Dreamhost account I also have console access. And, it's not a shared hosting. It depends... however, even if it's a private server, he needs to check "what can php do?". The terminal functionality is not relevant. Jan 25, 2013 at 12:46
2

try exec and also make sure safe mode off in your php.ini file like this safe_mode = Off

exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf')

also run this cmd in terminal to check if software is working

1

This is usually a function disabled by many webserver, you can check:

var_dump(ini_get('disable_functions')); // not available if shell_exec disabled
var_dump(ini_get('safe_mode')); // not available if true
0
0

Since You are running Linux you may have a rights Problem

  1. Check your file is owned by apache.

    chown apache apache file.php

  2. Check youir file has rights

    chmod 644 file.php

  3. Maybe check your sudoers file aswell
    Sudoers ManPage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.