PHP,找不到语法错误


PHP, cannot find syntax errors

EDIT:nvm,我的坏,基本上读错了重新报告的错误。记得检查日期并明智地清除日志。

Apache的日志在第2行、第5行和第23行报告了错误,分别是关于意外的"echo"、"switch"answers"回声"。我相信我已经检查了每一个可能缺失的分号,但我仍然找不到问题所在。

此外,不确定是否应在heredoc 后面添加分号

<?php
if($_FILES){
$image = $_FILES['filename']['name'];
switch ($_FILE['filename']['type']) {
    case 'image/jpeg': $ext = 'jpg';
        break;
    case 'image/png': $ext = 'png';
        break;
    case 'iamge/gif': $ext = 'gif';
        break;
    default: $ext = '';
        break;
}
if($ext)
    move_uploaded_file($_FILES['filename']['name'], "images/$image.$ext");
}
//$post_number = $post_number + 1;
$name = $_POST['name'];
$comment = $_POST['comment'];
$text = echo <<<_END
<article>
    //<h3> '$post_number'</h3>
    <h4>'$name'</h4>
    <br>
    <p> '$comment' </p>
    <img src=images/'$image.$ext'>
</article>
_END;
$file = fopen("index.php", 'r+');
fseek($file, -17, SEEK_END);
fwrite($text);
fclose($file);
?>

我看到了几个问题:

第1次:替换此行

$text = echo <<<_END

有了这个

$text = <<< "_END"

如果您想回显,请稍后执行echo $text。还要确保在结束行上除了_END;之外没有其他。没有选项卡,_END;前后没有空格(请参阅文档)。

2nd:您的fwrite将出错,因为它需要文件句柄,而不仅仅是要写入的文本。当你在写的时候,你应该检查文件是否成功打开,然后再写。用下面的代码替换你的文件操作:

if($file = fopen("index.php", 'r+')){
    fseek($file, -17, SEEK_END);
    fwrite($file, $text);
    fclose($file);
}else{/* Todo: handle fopen failure */}

快乐的编码。