PHP uploads files to the server and implements image preview

小云云
Release: 2023-03-22 10:24:02
Original
3503 people have browsed it

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(&#39;content-type:text/html,charset=gbk&#39;);
?>

<?php

    if(!empty($_FILES)){

        $current_img = $_FILES[&#39;lgc_img&#39;];
        $http_path = dirname(&#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;]).&#39;/images/&#39;.$current_img[&#39;name&#39;];

        $file_dir = dirname(__FILE__).&#39;/images/&#39;.$current_img[&#39;name&#39;];

        echo $current_img[&#39;tmp_name&#39;].&#39;<br/>&#39;;

        echo $file_dir;

        $bool = @move_uploaded_file($current_img[&#39;tmp_name&#39;],$file_dir);

    }
?>
Copy after login

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=&#39;<?php echo $http_path; ?>&#39; width=&#39;10%&#39; />
<?php
    }
?>
</body>

</html>
Copy after login

* 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!