我想在codeigniter中删除后从文件夹中删除文件

时间:2016-11-29 11:46:14

标签: php codeigniter

我不仅要删除数据库中的图像,还要删除文件夹中的图像。

这是我的控制器

import UIKit
import CoreData

class ExampleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

    @IBOutlet weak var tableView: UITableView!

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
    var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()

    var orders = [Order]()
    var startDate : NSDate = NSDate()
    var endDate : NSDate = NSDate()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchedResultController.delegate = self
        tableView.dataSource = self
        tableView.delegate = self

        fetchData()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return fetchedResultController.fetchedObjects!.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let order = fetchedResultController.fetchedObjects![section] as! Order
        return ((order.products?.count ?? 0) + (order.services?.count ?? 0))
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let textCellIdentifier = "ExampleTableViewCell"
        let row = indexPath.row

        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! ExampleTableViewCell

        let order = fetchedResultController.fetchedObjects![indexPath.section] as! Order // Data fetched using NSFetchedResultsController
        let products = (order.products?.allObjects ?? [Product]()) as! [Product] // Swift Array
        let services = (order.services?.allObjects ?? [Service]()) as! [Service] // Swift Array

        if (row < products.count) { // this is a Product row
            cell.orderLabel.text = products[row].name!
        } else { // this is a Service row
            cell.orderLabel.text = services[row-products.count].name!
        }
        return cell
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let order = fetchedResultController.fetchedObjects![section] as! Order
        return "\(order.date)"
    }


    func orderFetchRequest() -> NSFetchRequest {
        let fetchRequest = NSFetchRequest(entityName: "Order")
        let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
        let predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate) // startDate and endData are defined elsewhere

        fetchRequest.sortDescriptors = [sortDescriptor]
        fetchRequest.predicate = predicate

        return fetchRequest
    }

    func fetchData() {
        let fetchRequest = orderFetchRequest()
        fetchedResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath:nil, cacheName: nil)

        do {
            try fetchedResultController.performFetch()
        }
        catch let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
        }
    }
}

我可以从数据库中删除,但不能从文件夹中删除

2 个答案:

答案 0 :(得分:0)

请在删除功能中使用此代码

删除$ filename中的图片路径。

$filename = $record->picture; 
unlink("uploads/images/gallery/".$filename);

答案 1 :(得分:0)

 if (file_exists( base_url().'uploads/images/gallery/'.$filename))
    {
        unlink( base_url().'uploads/images/gallery/'.$filename);
    }