PHP does not need to upload images or download images,
The php ajax implementation code for the function of uploading images and downloading images without refreshing is as follows:
<Meta charset = "UTF-8"> <form id = "uploadForm"> <p> specify the file name: <input type = "text" name = "filename" value = ""/> </p> <p> upload a file: <input type = "file" name = "photo" onchange = "showPreview (this) "class =" file "/> </p> <input type =" button "value =" Upload "onclick =" doUpload () "/> </form> <script src =" http://www.haoyunyun.cn/jquery.js "> </script> <script> function doUpload () {var formData = new FormData ($ (" # uploadForm ") [0]); $. ajax ({url: 'submit. php ', type: 'post', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) {alert (returndata) ;}, error: function (returndata) {alert (returndata );}});} </script> <script type = "text/javascript"> function showPreview (source) {var file = source. files [0]; if (window. fileReader) {var fr = new FileReader (); fr. onloadend = function (e) {document. getElementById ("portrait "). src = e.tar get. result;}; fr. readAsDataURL (file) ;}</script>
Submit. php
<? Php if ($ _ FILES ['photo'] ['error']> 0) {echo "File Upload Failed"; die;} $ dir = '. /photo/'; $ type = substr ($ _ FILES ['photo'] ['name'], strrpos ($ _ FILES ['photo '] ['name'], '. '); $ filename = time (). rand (1000,9999 ). $ type; if (is_uploaded_file ($ _ FILES ['photo '] ['tmp _ name']) {move_uploaded_file ($ _ FILES ['photo '] ['tmp _ name'], $ dir. $ filename); echo "Upload succeeded";} else {echo "File Upload Failed ";}
Traverse database data
<? Php header ("content-type: text/html; charset = UTF-8"); $ link = mysql_connect ("127.0.0.1", 'root', 'root '); mysql_select_db ("php9", $ link); mysql_query ("set names utf8"); // query the total number of data records $ SQL = "select count (id) as count from upload "; $ arr = mysql_query ($ SQL); $ result = mysql_fetch_assoc ($ arr); // obtain the total number of items $ size = $ result ['Count']; // two pieces of data are displayed on each page $ length = 6; // calculate the number of pages $ pages = ceil ($ size/$ length ); $ page = isset ($ _ GET ['page'])? $ _ GET ['page']: 1; if ($ page <= 0) {$ page = 1;} if ($ page> $ pages) {$ page = $ pages;} // The number of data entries starting with $ start = ($ page-1) * $ length; $ SQL = "select * from upload limit $ start, $ length"; $ res = mysql_query ($ SQL);?> <Center> <table border = "1"> <div> <? Php while ($ a = mysql_fetch_assoc ($ res) {?> <Ul> <li> <? Php echo $ a ['id']?> </Li> <? Php echo $ a ['username']?> </Li> <a href = "photo. php" rel = "external nofollow"> "Width =" 80px "> </a> </li> <? Php echo $ a ['sc1']?> </Li> <a href = "photo3.php? Dir = <? Php echo $ a ['dir']?> "Rel =" external nofollow "> download </a> <a href =" photo4.php? Id = <? Php echo $ a ['id']?> & Dir = <? Php echo $ a ['dir']?> "Rel =" external nofollow "> Delete </a> </li> </ul> <? Php }?> </Div> </table> <a href = "photo2.php? Page = 1 "rel =" external nofollow "> homepage </a> <a href =" photo2.php? Page = <? Php echo $ page-1?> "Rel =" external nofollow "> previous page </a> <a href =" photo2.php? Page = <? Php echo $ page + 1?> "Rel =" external nofollow "> next page </a> <a href =" photo2.php? Page = <? Php echo $ pages?> "Rel =" external nofollow "> last page </a> </center> <style> * {margin: 0; padding: 0 ;}div {width: 900px; height: 850px; border: 1px solid #28a4c9; margin: auto;} img {width: 200px; height: 130px; margin-left: 100px;} ul {width: 400px; height: 300px; float: left;} li {list-style: none; margin-left: 10px;} </style>
Download Code
<?php header("content-type:text/html;charset=utf-8"); $dir=$_GET['dir']; $filename=substr($dir,strrpos($dir,'/')+1); header("Content-type:image"); header("content-disposition:attachment;filename=$filename"); readfile($dir); ?>
The above is a small Editor to introduce you to the function of uploading images and downloading images without refreshing ajax in PHP. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!