【问题标题】:Why image didn't appear and display in another page of php after submitted?为什么提交后图像没有出现并显示在php的另一个页面中?
【发布时间】:2013-05-02 03:51:17
【问题描述】:

我要设计2个php页面,一个是用户必须填写的个人详细信息表单,另一个是提交后在个人详细信息表单中显示所有个人详细信息。我的问题是已经提交的图像没有显示在第二页。我的代码有什么问题?我的代码如下所示:

<!DOCTYPE html>
<head>
<title></title>
//for preview a image 
   <script type="text/javascript">
 function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }
     </script>
    </head>
    <body>
         <form name="rform" method="get" action="researchers.php">  
        <h4>Researchers Profile</h4> 

         <fieldset>
    <legend>Personal Details</legend>
        <form id="form1" method="get" action="researchers.php" enctype="multipart/form-data" id="pro_image">
       <input type='file' onchange="readURL(this);" name="image" id="image"/>
        <br><img id="blah" src="profile pic.jpg" alt="your image" width="160px" height="120px"/><br/>
        </form>

     <input type="submit" name="savebtn" value="Save"/>
     </form>                

研究人员.php 页面

<?php

 $name=$_FILES['image']['name']; 
 $tmp=$_FILES['image']['tmp_name'];
 $new=time().$name;
 $new="upload/".$new;
 move_uploaded_file($tmp,$new);
 if($_FILES['image']['error']==0)
  {

 ?>
 <br /><img src="<?php echo $new;?>" width="100" height="100"/>
 <?php
 }
 ?>    

【问题讨论】:

  • 你有两次&lt;form ..
  • 好的,谢谢提醒...

标签: php html


【解决方案1】:

尝试将 &lt;form&gt; 方法更改为 post,如 $_FILES - http://www.php.net/manual/en/reserved.variables.files.php 的 php 手册中所述

说明

通过 HTTP POST 方法上传到当前脚本的项目的关联数组。

您还需要添加enctype="multipart/form-data" - http://www.w3.org/TR/html401/interact/forms.html#adef-enctype

<form name="rform" method="post" action="researchers.php" enctype="multipart/form-data">

另见http://www.php.net/manual/en/features.file-upload.post-method.php

【讨论】:

  • 您是否查看了输出的 html img 源代码 &lt;img src="..." /&gt; 以确保它是有效链接?您确定文件在upload 目录中吗?
  • 实际上在用户根据输入类型中的file选择他们的文件后,他们选择的文件将存储在默认目录中以检索图像文件并显示在researchers.php页面上。我不'真的不明白你在 的有效链接中的意思吗?我们是否必须手动创建存储图像/文件的目录?
猜你喜欢
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
相关资源
最近更新 更多