Home > Backend Development > PHP Problem > How to delete server video in php

How to delete server video in php

PHPz
Release: 2023-04-04 15:58:01
Original
438 people have browsed it

During the website development process, we may need to delete some video files on the server for various reasons. Today we will discuss how to use PHP language to delete video files on the server.

First of all, we need to clarify a few concepts:

  1. Server file path

The server file path refers to the storage path of the video file on the server . In Linux systems, absolute paths are generally used, such as /var/www/html/video/1.mp4; in Windows systems, relative paths can be used, such as .\video\1. mp4.

  1. File deletion function unlink()

PHP provides us with a function unlink() for deleting files, its syntax is as follows:

bool unlink ( string $filename [, resource $context ] )
Copy after login

Among them, $filename represents the file path and file name string to be deleted; $context is an optional parameter, which can be a context stream of resource type returned by stream_context_create().

Next, let’s take a look at how PHP uses the unlink() function to delete a video file:

<?php 
$file = "/var/www/html/video/1.mp4"; //服务器上视频文件路径 
if(unlink($file)) { //删除文件 
    echo "成功删除视频文件: ".$file; 
} else { 
    echo "无法删除视频文件: ".$file; 
} 
?>
Copy after login

The above PHP code first defines the file path to be deleted. Then, we use the unlink() function to delete the specified file. If the deletion is successful, "Video file successfully deleted: file path" is output on the page; if deletion fails, "Video file cannot be deleted: file path" is output.

It is worth noting that when the unlink() function accesses a file that does not exist, it will return false, so before deleting the file, we need to determine whether the file exists. We can use the file_exists() function that comes with PHP to determine whether a file exists, for example:

if (file_exists($file)) {
   // 如果文件存在,就执行删除操作 
   unlink($file); 
} else {
   // 如果文件不存在,则提示文件不存在。 
   echo "文件不存在,无法删除";
}
Copy after login

Finally, if we need to delete multiple video files, we can use a loop to achieve it. The following is a simple example to delete all video files in a specified directory:

<?php 
$dir = "/var/www/html/video/"; // 视频文件所在路径 
if (is_dir($dir)){ 
    if ($dh = opendir($dir)){ 
        while (($file = readdir($dh)) !== false){ 
            $file_path = $dir.$file; 
            if (is_file($file_path) && stripos($file, &#39;.mp4&#39;) !== false) { 
                unlink($file_path); 
            } 
        } 
        closedir($dh); 
    } 
} 
?>
Copy after login

In the above example, we first define the directory where the video files to be deleted are located. Then, we use PHP's built-in is_dir() function and opendir() function to verify whether the specified directory exists. If it exists, we use a while loop to traverse all files in the directory.

In each loop, we use the is_file() function to determine whether the current file is a video file (assuming here that the files we want to delete are all video files with an extension of .mp4), if so, we Call the unlink() function to delete the file.

Finally, we close the directory handle through the closedir() function.

In short, it is very convenient to use PHP to delete video files on the server. We only need to know the path of the file to be deleted, and the deletion operation can be easily achieved. At the same time, to ensure that the files we deleted were not accidentally deleted, we can use the file_exists() function to make judgments.

The above is the detailed content of How to delete server video in 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