Hướng dẫn php delete file
View Discussion Improve Article Save Article View Discussion Improve Article Save Article To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, $context ); Below programs illustrate the above approach: Program 1: This program uses unlink() function to remove file from directory. Suppose there is a file named as “gfg.txt” php
Output: gfg.txt has been deleted Program 2: This program uses unlink() function to delete a file from folder after using some operation. php
Output: Warning: unlink() expects parameter 1 to be a valid path, resource given in C:\xampp\htdocs\server.php on line 12 Resource id #3 cannot be deleted due to an error Note: If the file does not exist then it will display an error. PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
MySQL delete
Cấu trúc delete:
Xóa dữ liệuKiểu hướng đối tượng// Khai báo username $password = "123456"; // Khai báo password $server = "localhost"; // Khai báo server $dbname = "tintuc"; // Khai báo database // Kết nối database tintuc $connect = new mysqli($server, $username, $password, $dbname); //Nếu kết nối bị lỗi thì xuất báo lỗi và thoát. if ($connect->connect_error) { die("Không kết nối :" . $connect->connect_error); exit(); } //Code xử lý, xóa record dữ liệu của table dựa theo điều kiện WHERE tại id = 1 $sql = "DELETE FROM tin_xahoi WHERE id=1"; //Nếu kết quả kết nối không được thì xuất báo lỗi và thoát if ($connect->query($sql) === TRUE) { echo "Dữ liệu đã được xóa"; } else { echo "Lỗi delete: " . $connect->error; } //Đóng kết nối database tintuc $connect->close(); ?> Ứng với điều
kiện Kiểu thủ tục// Khai báo username $password = "123456"; // Khai báo password $server = "localhost"; // Khai báo server $dbname = "tintuc"; // Khai báo database // Kết nối database tintuc $connect = mysqli_connect($server, $username, $password, $dbname); //Nếu kết nối bị lỗi thì xuất báo lỗi và thoát. if (!$connect) { die("Không kết nối :" . mysqli_connect_error()); exit(); } //Code xử lý, xóa record dữ liệu của table dựa theo điều kiện WHERE tại id = 1 $sql = "DELETE FROM tin_xahoi WHERE id=1"; //Nếu kết quả kết nối không được thì xuất báo lỗi và thoát if (mysqli_query($connect, $sql)) { echo "Dữ liệu đã được xóa"; } else { echo "Lỗi delete: " . mysqli_error($connect); } //Đóng database mysqli_close($connect); ?> Download file ví dụ Trong file download đã có sẵn file tintuc.sql, file này là file dữ liệu mẫu, sau khi đã tạo database chúng ta có thể đưa dữ liệu từ file tintuc.sql bằng thao tác import có trong phpMyAdmin. |