1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I convert this working code on Windows to Linux?

Discussion in 'PHP' started by stylusss, Mar 25, 2017.

  1. #1
    I've been trying to figure this out for the life of me: I asked the question on stackoverflow, but they weren't able to help:

    http://stackoverflow.com/questions/42967583/executing-a-linux-program-from-php

    <?php
    
    $file = 'C:\xampp\htdocs\SMF\sdsdssss.docx';
    
    $content = exec( 'C:\xampp\htdocs\SMF\doctotext\doctotext.exe /c '. $file , $output );
    
    var_dump( $output );
    Code (markup):
    How do I write this code on a linux server (Centos x86_64)?

    I'm using this free software: http://silvercoders.com/en/products/doctotext/

    The first person to get an answer will get $20 USD to their PayPal. I tried:

    $file = '/home/***/public_html/downloads/test_doc.docx';
    
    $content =  exec('/home/***/public_html/doctotext/doctotext.sh \c '. $file, $output, $return);
    Code (markup):
     
    stylusss, Mar 25, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    And, what exactly does that return, in the php-error-log, other error-logs, or anything at all? Besides, I'm guessing using -c or --c instead of \c might help?

    As long as the exec() works on your server, running a .sh shouldn't be any different, although the syntax and output might be slightly different.
     
    PoPSiCLe, Mar 25, 2017 IP
  3. stylusss

    stylusss Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hi

    No PHP errors, unfortunately.

    var_dump( $content ): string(0) "" 
    var_dump( $output ): array(0) { } 
    var_dump( $return ): int(126) 
    var_dump( $string ): string(0) "" 
    Code (markup):
    If I give you FTP access, could you try it? I've asked everyone, and no help
     
    stylusss, Mar 25, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Have you tried running the file manually, from the command-line (SSH) to see if it works as intended there?
     
    PoPSiCLe, Mar 25, 2017 IP
  5. stylusss

    stylusss Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    I have not.

    Could you give me a sample code? AKA what should I type into the command line?
     
    stylusss, Mar 25, 2017 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    How am I supposed to know? I don't know the program, or the syntax - I'm generalising here. Read the documentation, or the man page, or try running the program with a -help modifier or similar. If you use a non-existing modifier, it will normally list the available modifiers.
     
    PoPSiCLe, Mar 25, 2017 IP
  7. stylusss

    stylusss Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    I understand but I linked the software and its free to download. I was hoping someone could try it on their server.
     
    stylusss, Mar 25, 2017 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    I think you are trying to read a docx file into a text string. This code below works. It doesn't use the software you are using...
    Here's the output: http://www.imrobos.com/dd/1.php
    Here's the docx file: http://www.imrobos.com/dd/aa.docx

    Any reason you want to use the third party software?

    <?php
    
    function docx2text($filename) {
       return readZippedXML($filename, "word/document.xml");
    }
    function readZippedXML($archiveFile, $dataFile) {
    // Create new ZIP archive
    $zip = new ZipArchive;
    // Open received archive file
    if (true === $zip->open($archiveFile)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $xml = new DOMDocument();
        $xml->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags
            return strip_tags($xml->saveXML());
        }
        $zip->close();
    }
    // In case of failure return empty string
    return "";
    }
    
    echo docx2text( "aa.docx"); // Save this contents to file
    ?>
    PHP:
     
    JEET, Mar 28, 2017 IP
  9. stylusss

    stylusss Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Reason is because I need a full solution for all document types, not just docx.

    I found this package on GitHub (doctotext). I'd like to install it on my Centos-running dedicated server (x86_64), but I do not know how. Could someone offer instructions as to how I could do it. There are no instructions provided, unfortunately
     
    stylusss, Mar 29, 2017 IP
  10. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #10
    Did you check your phpinfo file? Generally commands like exec, passthrough etc are in the "disabled_functions" list.
     
    JEET, Mar 29, 2017 IP
  11. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #11
    Hi,
    I downloaded the package you linked, and it has a "makefile" inside the "src" directory.
    You need to install the doctotext using this first.
    Unzip the package, go to src directory, and run commands:
    configure
    make
    make install

    I've never installed a package on linux myself, but found this link that might help:
    http://www.linuxquestions.org/questions/linux-newbie-8/help-how-to-install-a-makefile-872181/

    You can hire someone to do this for you, but make sure its a genuine, trustworthy person since you will give him root access...
    Why don't you contact your webhost? May be they will install this for a fee?

    Once done, they can just use commandline to check if its extracting text from one of the sample files that are in the downloaded zip.
    From there on you can figure out the PHP code easily...
     
    JEET, Mar 29, 2017 IP
  12. stylusss

    stylusss Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    I'm the owner of the dedicated server, so there isn't a host, unfortunately.

    Still waiting for someone to try it out before I do.

    Thank you for your assistance though
     
    stylusss, Mar 29, 2017 IP
  13. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #13
    I don't think you can run a .EXE file directly on linux.
    You'll need some other third party binary anyways.
    https://www.techperiod.com/install-windows-exe-files-linux/

    I think that is the problem. The EXE is not getting executed.
    This is why I asked you to install the doctotext binary first, and then try your existing code. Instead of location to .exe file, give location to this doctotext binary in the exec command.

    You can hire someone from here, or from stack, or from freelancer who can do the install and run a test from commandline
     
    JEET, Mar 29, 2017 IP
    Rukbat likes this.
  14. yetta

    yetta Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #14
    Hi, not sure if this will help, but Windows and Linux use different line endings in their files which can cause issues with code being executed on a different environment.

    Windows uses CRLF while Linux uses LF, check in your editor for settings to save your files as LF. I use Atom Editor for coding, it has an option to automatically save to a set format, i code everything with LF as it works on both systems without issue.

    Also double check the file paths in the code and that your server stack is properly set in the Linux environment paths.

    Sidenote I'm a noobie, but faced these issues before on a Linux server, those changes helped me.
     
    yetta, Mar 30, 2017 IP
  15. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #15
    Jeet's "I don't think you can run a .EXE file directly on linux." has only one error - it's not "I don't think", it's "you can't". You'll have to find a Linux program that does what
    doctotext.exe does, or just write the function of that program inside your PHP program (in PHP, of course). PHP can read an xml file, so it's pretty trivial. (I know exactly how trivial it is - I've written it. Unfortunately, every time I did, it was proprietary, so I can't give you the code, but simplexml_load_string($XMLData) should be easy enough to figure out.)
     
    Rukbat, Apr 9, 2017 IP
    JEET likes this.
  16. Freddie12

    Freddie12 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    They have a new version out for both windows and linux, I am using it on windows but there is documentation in both so you should be fine: https://sourceforge.net/projects/doctotext/
     
    Freddie12, Feb 24, 2023 IP