回声显示它不应该是什么时候

时间:2014-09-11 15:42:37

标签: php html

您好我的代码有问题由于某种原因,当我加载文件时它显示启动时有关如何解决此问题的想法?     

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<a href='http://$url/files/".  basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999999999999">
<input name="uploadedfile" type="file"><br>
<input type="submit" value="Upload">
</form>

3 个答案:

答案 0 :(得分:3)

我只是回答一下,看看OP accepts it是否可以解决问题; since it did work for OP.

将提交按钮命名为<input type="submit" value="Upload" name="submit">,然后将PHP包装在if(isset($_POST['submit'])){...}


另一种方法是使用两个单独的文件。

例如。 HTML表单

<form enctype="multipart/form-data" action="handler.php" method="POST">...</form>

handler.php (同时保留指定的提交按钮)

if(isset($_POST['submit'])){

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "<a href='http://$url/files/".  basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
    } else{
        echo "There was an error uploading the file, please try again!";
    }

} // brace for if(isset($_POST['submit']))

答案 1 :(得分:1)

echo显示因为你有一个if语句而且在加载页面上它总是错误的。首先在ifset中添加一个if:

 if(isset($_POST['uploadedfile'])){
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
            echo "<a href='http://$url/files/".  basename( $_FILES['uploadedfile']['name'])."'>Download</a>";
        } else{
            echo "There was an error uploading the file, please try again!";
        }
    }
        ?>
        <form enctype="multipart/form-data" action="" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="99999999999999999">
        <input name="uploadedfile" type="file"><br>
        <input type="submit" value="Upload">
        </form>

如果不发布更多信息/代码并且特定的女巫回声显示... thx

,应该做的伎俩

将名称添加到名称=&#34;提交&#34;按钮会更清晰并在按钮名称上执行isset或输入类型=&#34;隐藏&#34;带有表格的名称。

答案 2 :(得分:1)

您从不打扰检查是否实际执行了POST,并且您也不必检查上传是否实际成功。 从不假设成功。总是假设失败并将成功视为一个惊喜:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ($_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
        die("Upload failed with error code {$_FILES['uploadedfile']['error']}");
   }
   ... do your upload processing here
}