2014-02-19 63 views
-1

我正在实现一个表单来获取用户的个人详细信息和DP用户的图像。如果数据正确提交,我如何在下一页显示它。如何获取图像并将其显示在下一页上?

+1

你保存它时,表单被提交,并在您检索时提交想展示它。如需更具体的回复,请提出更具体的问题(代码?)。 – CompuChip

回答

1

这可以帮助你,

上传表单。

<html> 
    <head><title>File Upload To Database</title></head> 
    <body> 
    <h2>Please Choose a File and click Submit</h2> 
    <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="99999999" /> 
    <div><input name="userfile" type="file" /></div> 
    <div><input type="submit" value="Submit" /></div> 
    </form> 

</body></html> 

上传图片。

<?php 
/*** check if a file was submitted ***/ 
if(!isset($_FILES['userfile'])) 
    { 
    echo '<p>Please select a file</p>'; 
    } 
else 
    { 
    try { 
     upload(); 
     /*** give praise and thanks to the php gods ***/ 
     echo '<p>Thank you for submitting</p>'; 
     } 
    catch(Exception $e) 
     { 
     echo '<h4>'.$e->getMessage().'</h4>'; 
     } 
    } 
?> 

上传功能

<?php 
/** 
* 
* the upload function 
* 
* @access public 
* 
* @return void 
* 
*/ 
function upload(){ 
/*** check if a file was uploaded ***/ 
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false) 
    { 
    /*** get the image info. ***/ 
    $size = getimagesize($_FILES['userfile']['tmp_name']); 
    /*** assign our variables ***/ 
    $type = $size['mime']; 
    $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb'); 
    $size = $size[3]; 
    $name = $_FILES['userfile']['name']; 
    $maxsize = 99999999; 


    /*** check the file is less than the maximum file size ***/ 
    if($_FILES['userfile']['size'] < $maxsize) 
     { 
     /*** connect to db ***/ 
     $dbh = new PDO("mysql:host=localhost;dbname=testblob", 'username', 'password'); 

       /*** set the error mode ***/ 
       $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      /*** our sql query ***/ 
     $stmt = $dbh->prepare("INSERT INTO testblob (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)"); 

     /*** bind the params ***/ 
     $stmt->bindParam(1, $type); 
     $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB); 
     $stmt->bindParam(3, $size); 
     $stmt->bindParam(4, $name); 

     /*** execute the query ***/ 
     $stmt->execute(); 
     } 
    else 
     { 
     /*** throw an exception is image is not of type ***/ 
     throw new Exception("File Size Error"); 
     } 
    } 
else 
    { 
    // if the file is not less than the maximum allowed, print an error 
    throw new Exception("Unsupported Image Format!"); 
    } 
} 
?> 

显示图像

<?php 

/*** some basic sanity checks ***/ 
if(filter_has_var(INPUT_GET, "image_id") !== false && filter_input(INPUT_GET, 'image_id', FILTER_VALIDATE_INT) !== false) 
    { 
    /*** assign the image id ***/ 
    $image_id = filter_input(INPUT_GET, "image_id", FILTER_SANITIZE_NUMBER_INT); 
    try  { 
     /*** connect to the database ***/ 
     $dbh = new PDO("mysql:host=localhost;dbname=testblob", 'username', 'password'); 

     /*** set the PDO error mode to exception ***/ 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     /*** The sql statement ***/ 
     $sql = "SELECT image, image_type FROM testblob WHERE image_id=$image_id"; 

     /*** prepare the sql ***/ 
     $stmt = $dbh->prepare($sql); 

     /*** exceute the query ***/ 
     $stmt->execute(); 

     /*** set the fetch mode to associative array ***/ 
     $stmt->setFetchMode(PDO::FETCH_ASSOC); 

     /*** set the header for the image ***/ 
     $array = $stmt->fetch(); 

     /*** check we have a single image and type ***/ 
     if(sizeof($array) == 2) 
      { 
      /*** set the headers and display the image ***/ 
      header("Content-type: ".$array['image_type']); 

      /*** output the image ***/ 
      echo $array['image']; 
      } 
     else 
      { 
      throw new Exception("Out of bounds Error"); 
      } 
     } 
    catch(PDOException $e) 
     { 
     echo $e->getMessage(); 
     } 
    catch(Exception $e) 
     { 
     echo $e->getMessage(); 
     } 
     } 
    else 
     { 
     echo 'Please use a real id number'; 
     } 
?> 

如果你需要的任何其他信息,您canrefer此链接:

http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html 
+0

谢谢Shivam .... –

+0

没关系,请接受这个答案。 – Shivam

1

传递页面之间的值有很多方法。

  1. 存储会话和使用下一页。如果不使用,则稍后删除该会话
  2. 如果您使用Ajax表单发布获取详细信息,则事情更简单。获取详细信息并存储在会话中或任何隐藏的表单参数。发布到下一页。
相关问题