前言
php案例 文件上传并预览功能
代码
cyg.php
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="2.php" method="POST" enctype="multipart/form-data"> <!--<input type="hidden" name="MAX_FILE_SIZE" value="30000" />--> Send this file: <input name="file" type="file" /> <input type="submit" value="提交" /> </form> </script> </body> </html>
2.php
<?php $file=dirname(__FILE__); $file1=str_replace('\\', '/',$file); echo move_uploaded_file($_FILES['file']['tmp_name'],$file1."/uploads/".basename($_FILES['file']['name'])); //意思是把上传的文件放到D:\phpstudy_pro\WWW\uploads文件夹里面 $file2=$file1."/uploads/".basename($_FILES['file']['name']);//获取上传的文件的绝对路径 $file4=file_get_contents($file1."/uploads/".basename($_FILES['file']['name'])); //获取上传文件的内容,并在下面输出出来 echo $file4; //print_r($file3);
效果:也就是上传的文件里面的内容