【问题标题】:Why is my php script not echoing the result into the original page?为什么我的 php 脚本没有将结果回显到原始页面中?
【发布时间】:2016-08-20 01:03:33
【问题描述】:

我目前有以下代码,用于包含上传表单的页面

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />

</form>

<? include('uploader.php'); ?>

然后我将文件 uploader.php 保存在同一目录中。该文件包含以下代码:

<?php

if( $_POST ){
// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path .time() .basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The  <a href=" . $target_path . ">file</a> has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

}

?>

我的意图是让它在同一页面上回显"The &lt;a href=" . $target_path . "&gt;file&lt;/a&gt; has been uploaded";,但它会重定向到另一个页面以显示此消息。

为什么会这样?

【问题讨论】:

  • 它重定向到表单操作上使用的页面
  • 谢谢小龙。我删除了表单操作位,现在它可以工作了。

标签: php html file-upload upload uploader


【解决方案1】:

您已将表单操作设置为 uploader.php,因此一切都发生在那里,因此当您单击提交按钮时,它将加载该文件并在该页面上显示您的回显,因此与您开始的页面不同。

【讨论】:

    【解决方案2】:

    试试看:-

    <form enctype="multipart/form-data" action="" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
        Choose a file to upload: <input name="uploadedfile" type="file" /><br />
        <input type="submit" value="Upload File" />
    
        </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 2012-08-09
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多