Shell_exec does not work, cannot convert pdftotext

I am trying to convert a pdf file to a text file. When I run the command through the terminal, it works fine, but when I try to execute it through PHP it does not work.

I’m stuck in this situation for the last four hours, I spend a lot of time on Google, but the solution is not available. Can anyone solve this problem?

File Owner - None

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

Can anyone provide any useful solution?

I also tried changing the owner of the usr folder from root to nobody and granting 777 permission to the folder and its context.

+4
source share
5 answers

Your command to start pdftotext is incorrect.

There should be a second argument indicating pdftotext to write to a specific file or just use the dash ā€œ-ā€ to write to stdout, if you really do not want the program to create a text file with the file name as pdf (this will require write permissions in the folder /opt/lampp/.../)

This is at least true for pdftotext version 0.12.4

"Pdftotext reads a PDF file, a PDF file, and writes a text file, a text file. If no text file is specified, pdftotext converts the .pdf file to file.txt. If the text file is" - ", the text is sent to standard output."

So, solving your question will simply add a dash after the file name, for example:

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

Given the existence of the binary and PHP, shell_exec is allowed to use, and you have permissions and that the pdf file exists, and you have permissions.

+5
source

from how to check if the PHP system () function is allowed? and not disabled 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 feature may be disabled.

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

+2
source

try exec, and also disable safe mode in the php.ini file, like this safe_mode = Off

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

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

+2
source

This is usually a feature disabled by many web servers, 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 
+1
source

Since you are using Linux, you may have rights issues

  • Verify that your file belongs to apache.

    chown apache apache file.php

  • Check that youir file has permissions

    chmod 644 file.php

  • Maybe check the sudoers file as well as the Sudoers ManPage

0
source

All Articles