I am implementing a form to get personal details of user and an image of user for DP. How do i display it on next page if data is submitted correctly.
2条回答 默认 最新
- doucao1066 2014-02-19 10:27关注
This can help you,
The upload form.
<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>
Uploading the image.
<?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>'; } } ?>
The upload function
<?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!"); } } ?>
Displaying the image
<?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'; } ?>
If you want any other information, you canrefer to this link:
http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 基于卷积神经网络的声纹识别
- ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
- ¥100 为什么这个恒流源电路不能恒流?
- ¥15 有偿求跨组件数据流路径图
- ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
- ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
- ¥15 CSAPPattacklab
- ¥15 一直显示正在等待HID—ISP
- ¥15 Python turtle 画图
- ¥15 stm32开发clion时遇到的编译问题