In the process of background development, it is often necessary for the application to upload files to the server. This article mainly shares with you PHP to upload files to the server and implement image preview. I hope it can help everyone.
1. Backend request address page operation (can also be the current page):
<?php header('content-type:text/html,charset=gbk'); ?> <?php if(!empty($_FILES)){ $current_img = $_FILES['lgc_img']; $http_path = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/images/'.$current_img['name']; $file_dir = dirname(__FILE__).'/images/'.$current_img['name']; echo $current_img['tmp_name'].'<br/>'; echo $file_dir; $bool = @move_uploaded_file($current_img['tmp_name'],$file_dir); } ?>
2. Frontend form page:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 上传文件: <input type="file" name="lgc_img" /> <input type="submit" name="submit" value="提交" /> </form> <?php if(@$bool) { ?> <img src='<?php echo $http_path; ?>' width='10%' /> <?php } ?> </body> </html>
* This case action="" indicates the requested current page
* Summary: $_FILES and move_uploaded_file() are mainly used
The above is the detailed content of PHP uploads files to the server and implements image preview. For more information, please follow other related articles on the PHP Chinese website!