Hướng dẫn php delete file

View Discussion

Show

Improve Article

Save Article

  • Read
  • Discuss
  • 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

    $file_pointer = "gfg.txt";

    if (!unlink($file_pointer)) {

        echo ("$file_pointer cannot be deleted due to an error");

    }

    else {

        echo ("$file_pointer has been deleted");

    }

    ?>

    Output:

    gfg.txt has been deleted

    Program 2: This program uses unlink() function to delete a file from folder after using some operation. 

    php

    $file_pointer = fopen('gfg.txt', 'w+');

    fwrite($file_pointer, 'A computer science portal for geeks!');

    fclose($file_pointer);  

    if (!unlink($file_pointer)) {

        echo ("$file_pointer cannot be deleted due to an error");

    }

    else {

        echo ("$file_pointer has been deleted");

    }

    ?>

    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.


    • Trang chủ
    • Hướng dẫn học
    • Học PHP
    • MySQL delete

    MySQL delete

    • Khi cần xóa dữ liệu nào đó không cần thiết, ta có thể sử dụng câu lệnh DELETE trong MySQL để thực hiện việc xóa.
    • Các bước thực hiện:

      • Kết nối database và table.
      • Xác định record (hàng) cần xóa.
      • Xử lý dữ liệu.
      • Đóng database.

    Cấu trúc delete:

    • tên_table: là tên bảng được chọn xem dữ liệu.
    • tên_cột: là tên các cột có trong table.
    • giá_trị_cột_cần_xóa: giá trị nhận biết record (hàng) cần xóa.

    Xóa dữ liệu

    Kiể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 WHERE id=1, tương ứng với record (hàng) thứ 1, tất cả dữ liệu của record này sẽ bị xóa.

    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.