Home > Backend Development > PHP Problem > How to quickly build a picture server using PHP

How to quickly build a picture server using PHP

PHPz
Release: 2023-04-11 10:48:04
Original
1396 people have browsed it

PHP is a server-side scripting language widely used in web development. In many practical applications, we need to build some servers to specifically store and manage images for reference and display in our applications and websites. This article introduces how to use PHP to quickly build a picture server.

  1. Basic environment installation

Before we start, we need to install PHP and related extension libraries. In Linux systems, you can install it through the following command:

sudo apt-get install -y php php-gd php-mbstring php-xml php-zip
Copy after login

Among them, the php-gd extension library is used to process images, and the php-mbstring extension library is used to To process strings, the php-xml extension library is used to process XML files, and the php-zip extension library is used to process compressed files.

  1. Create Folder

Next, we need to create a directory on the server to store images. You can execute the following command on the command line:

mkdir /var/www/html/images
Copy after login

Among them, /var/www/html is the default website root directory of the Apache server, and images is the one we will create Directory, if necessary, can be modified according to the actual situation.

  1. Upload pictures

Next, open a file manager on the local computer and drag the image file to be uploaded to images in the service directory folder.

  1. Write the upload script

Create an upload.php file on the server to implement the image upload function. Enter the following code in the file:

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// 检查文件是否是图片类型
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if ($check === false) {
        echo "文件不是图片类型。";
        $uploadOk = 0;
    }
}

// 检查文件是否已经存在
if (file_exists($target_file)) {
    echo "该文件已经存在。";
    $uploadOk = 0;
}

// 检查文件大小
if ($_FILES["fileToUpload"]["size"] > 5000000) { // 5MB
    echo "文件大小超出限制。";
    $uploadOk = 0;
}

// 允许上传的文件类型
if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif") {
    echo "仅支持上传jpg,jpeg,png和gif文件。";
    $uploadOk = 0;
}

// 如果有任何错误,则中止上传过程
if ($uploadOk == 0) {
    echo "上传失败。";
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "文件" . basename($_FILES["fileToUpload"]["name"]) . "上传成功。";
    } else {
        echo "上传文件时发生了错误。";
    }
}
?>
Copy after login

The above code is relatively simple. It mainly implements some basic checks on uploaded images, including: checking whether the file is an image type and checking whether the file already exists. , check the file size and file types allowed to be uploaded, etc. If it meets the requirements, save the image to the images directory.

  1. Test the upload script

Enter the following URL in the browser address bar to access the upload script:

http://[your_server_ip]/upload.php
Copy after login

Among them, [your_server_ip] is the IP address of your server. On the upload page, select the image file on your local computer and press the "Upload" button to upload the image to the images directory on the server.

  1. Get pictures

If you need to get pictures on the server in a web application, you can create a simple PHP script to output the pictures. Create a file called image.php on the server, fill it with the following code:

<?php
// 获取图片文件名
$image_name = $_GET[&#39;name&#39;];

// 图片文件路径
$image_path = &#39;images/&#39; . $image_name;

// 生成响应头
header("content-type: image/jpeg");
header(&#39;Content-Disposition: inline; filename="&#39; . $image_name . &#39;"&#39;);
header(&#39;Content-Length: &#39; . filesize($image_path));

// 输出图片
echo file_get_contents($image_path);
?>
Copy after login

What the above PHP script does is get the file name sent from the client request and check Whether the image file exists. If it exists, output the image to the client and pause the loading of other pages to allow the image to fully load.

  1. Display images

To display images in a web application, you can use the <img> tag in the HTML page. The following is an HTML code example:

<!DOCTYPE html>
<html>
<head>
    <title>图片展示</title>
</head>
<body>
<h1>图片展示</h1>
<img src="http://[your_server_ip]/image.php?name=myimage.jpg" alt="My Image">
</body>
</html>
Copy after login

In the above code, http://[your_server_ip]/image.php represents the address of the PHP script, name=myimage.jpg represents the image file name to be displayed. Images can be displayed in web applications through the <img> tag.

Conclusion

In this article, we introduce how to use PHP to quickly build a picture server to easily manage and display pictures. By creating an image storage directory on the server and writing basic upload, retrieval and output scripts, we can build a powerful image server without any complicated settings.

The above is the detailed content of How to quickly build a picture server using PHP. For more information, please follow other related articles on the PHP Chinese website!

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