1

Here's what the situation is: I am trying to use PHP to call scanimage on a Linux host, then save the resulting file to a web directory for future use.

The below code produces no errors, but when I check out the /tmp directory, file.pnm is blank, and the scanner never starts.

<?php
require('/var/www/olin/includes/functions.php');
$con = connect_db();

//setup the POST variables
if (isset($_POST['submit'])) {
    $fname = mysqli_real_escape_string($con, $_POST['fname']);
    $lname = mysqli_real_escape_string($con, $_POST['lname']);
    $license_no = mysqli_real_escape_string($con, $_POST['license_no']);
    $comments = mysqli_real_escape_string($con, $_POST['comments']);
} 


if ($license_no == '') { 
        $license_no = "None on File";
}

if ($fname == '' || $lname == '') { 
        echo '<h1 class="message">Can\'t submit visitor: you are missing information!</h1>'; 
} else {

    //setup the query and prepare it for exection
    $query=     "insert into visitors (fname, lname, license_no, redsheet, comments)" .
        " values (?, ?, ?, 'Allow', ?) on duplicate key update fname = values(fname)," .
        "lname = values(lname), license_no = values(license_no), redsheet = values(redsheet)," .
            "comments = values(comments)";
    $stmnt=     mysqli_prepare($con, $query);

    //bind the statement parameters to variables
    mysqli_stmt_bind_param($stmnt, "ssss", $fname, $lname, $license_no, $comments );

    //execute, then close the statment
    if (!mysqli_stmt_execute($stmnt)) {
        echo "Failed to ececute the query: " . mysqli_error($con);
        header('Refresh: 10; url=http://localhost/olin/visitor.php');
    } 
}

// we'll `try` to scan the license  if the checkbox is selected
if (isset($_POST['pic_id'])) { 
    // get the info from the db
    $query = 'select id from visitors where license_no = "'.$license_no.'"';

    $result = mysqli_query($con, $query);
    while ($row = mysqli_fetch_array($result)) {
        $id = $row['id'];

        // set up the path to save the id to (and put path into db for further look    up
        // and display)
        $dir = ( $id % 30 );

        $path = '/var/www/olin/images/licenses/'.$dir.'/'.$id.'-license.png';
        $path = addslashes(mysqli_real_escape_string($con, $path));
        $path1 = '/images/licenses/'.$dir.'/'.$id.'-license.png';

        // start the scan, and save image
        $command = '/home/jmd9qs/bin/scan.sh "'.$path.'"';

        $update = 'update visitors set id_pic = "'.$path1.'" where id="'.$id.'"';
        mysqli_query($con, $update) or die ('Error: ' . mysqli_error($con));
        exec($command);
        header('Location: http://localhost/olin/visitor.php');
    }
}

?>

Can anybody provide any hints?

UPDATE:

I have the server running the command now (I know it's failing because of the Apache2 error log).

Here's the error I get: scanimage: open of device brother3:bus2;dev1 failed: Invalid argument

I've tried adding the www-data user to the scanner and lp groups, but it seems to have no effect... The scanimage command I'm using works under my normal user and as root, so I'm now positive the command I'm using should work. I am still at a loss...

UPDATE (again):

I've fixed some errors in my code... now the server will scan and successfully save images! However, it only works once and then for some odd reason I have to run the scan.sh (which I put the scanimage command into) through my shell for it to run again... otherwise I get the same error message!

Very weird, I have NO clue why, suggestions wanted!

4

0 回答 0