将图像保存为会话并显示它

时间:2014-11-10 00:12:28

标签: php html image

我正在尝试将文件/图像保存为会话,然后显示它..

这是包含表格的第一页:

  <form method="post">
   <input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
           </form> 

好的,接下来我将图像保存为会话并尝试显示(不成功)...

代码:

        <?php 
      if (isset( $_POST['picture'])) 

        $_SESSION['picture'] = $_POST['picture'];

        echo  "<img src=" $_SESSION['picture'] " border='0' /> " 
          ?>

1 个答案:

答案 0 :(得分:1)

我不知道你会做什么,但这是最适合的方法

当您在表单中放置文件类型时,需要使用全局变量$ _FILES

<强> form.html

 <form action="process.php" method="post" enctype="multipart/form-data">
  <label for="picture">Picture:</label>
  <input type="file" name="picture" id="picture"><br>
  <input type="submit" name="submit" value="Upload">
</form>

<强> process.php

<?php
    session_start();
    //make sure you have created the **upload** directory

    $filename    = $_FILES["picture"]["tmp_name"];
    $destination = "upload/" . $_FILES["picture"]["name"]; 
    move_uploaded_file($filename, $destination); //save uploaded picture in your directory

    $_SESSION['picture'] = $destination;

    header('Location: display_picture.php');

<强> display_picture.php

<?php
session_start();
?>

<div>
  <img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>