0
votes

I have a Ajax uploader on my page. It worked fine since yesterday. Now I get an Error 500. In console it says

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

and refers to my upload script:

  <?php
// A list of permitted file extensions
$allowed = array('html','htm');

function save_table_to_json ( $in_file, $out_file ) {
    $html = file_get_contents( $in_file );
    file_put_contents( $out_file, convert_table_to_json( $html ) );
}

function convert_table_to_json ( $html ) {
$document = new DOMDocument();
$document->loadHTML( $html );

$xpath = new DomXPath($document);
$tables = $xpath->query("//*[contains(@class, 'mon_list')]");
$tableDom = new DomDocument();
$tableDom->appendChild($tableDom->importNode($tables->item(0), true));

$obj = [];
$jsonObj = [];
$th = $tableDom->getElementsByTagName('th');
$td = $tableDom->getElementsByTagName('td');
$thNum = $th->length;
$arrLength = $td->length;
$rowIx = 0;

for ( $i = 0 ; $i < $arrLength ; $i++){
    $head = $th->item( $i%$thNum )->textContent;
    $content = $td->item( $i )->textContent;
    $obj[ $head ] = $content;
    if( ($i+1) % $thNum === 0){ 
        $jsonObj[++$rowIx] = $obj;
        $obj = [];
    }
}

    return json_encode([ "Values" => $jsonObj ]);
}


if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error1"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], $_FILES['upl']['name'])){
        save_table_to_json( 'heute_S.htm', 'heute_S.json' );
        save_table_to_json( 'heute_L.htm', 'heute_L.json' );
        save_table_to_json( 'morgen_S.htm', 'morgen_S.json' );
        save_table_to_json( 'morgen_L.htm', 'morgen_L.json' );
        echo '{"status":"success1"}';
    }else{
    echo '{"status":"error"}';
    exit;
}
}

I didn´t change anything on server. In server legit only says there was a error 500.

But how can I fix that ?


Edit:

In Log it says:

[Wed Dec 02 17:31:26.625826 2015] [fcgid:warn] [pid 11680] [client IP] mod_fcgid: stderr: PHP Warning: DOMDocument::loadHTML(): Empty string supplied as input in /upload.php on line 12, referer: /

The second:

[Wed Dec 02 17:31:26.625830 2015] [fcgid:warn] [pid 11680] [client IP] mod_fcgid: stderr: PHP Catchable fatal error: Argument 1 passed to DOMDocument::importNode() must be an instance of DOMNode, null given, called in /upload.php on line 7 and defined in /upload.php on line 17, referer: /

1
what have you before the error? - jameshwart lopez
it worked fine with the same script no changes. - Marius Schönefeld
and where is your ajax uploader code? we would see that too. - Jai
these are the codes I use template. Minot familiar with Ajax. - Marius Schönefeld
The error 500 is caused by the server, not the client. You must to share your server code, not the javascript. you must to check your error log in the apache server and then you will know what happens automatically - Marcos Pérez Gude

1 Answers

1
votes

Check your webserver error logs. Depending on webserver, should be in either /var/log/apache or /var/log/nginx.

Those will give you information about what's actually going wrong, and you should be able to resolve it easily enough from there.