开发者

Deleting a file in afterDelete() - CakePHP

I have an Upload model - contains images uploaded by the user.

Within the controller, I'm calling:

$this->Upload->delete($id);

Which works just fine to delete the record from the database, but I'd also like to delete the corresponding image file.

I've tried a lot of different things with different开发者_StackOverflow variables..etc, and found this link to a similar question, but - nothing seems to work - I can't even get it to delete with any of these attempts:

//at the bottom of my Upload model
function afterDelete() {
    //NONE OF THESE WORK - is this even being called?
    unlink('/img/uploads/rest_logo.jpg');
    unlink('app/webroot/img/uploads/rest_logo.jpg');
    unlink('/app/webroot/img/uploads/rest_logo.jpg');
    unlink('img/uploads/rest_logo.jpg');
    unlink(WWW_ROOT . 'img/uploads/rest_logo.jpg');
    unlink(WWW_ROOT . '/img/uploads/rest_logo.jpg');
    return true;
}

Obviously I'll want to change it to be a dynamic filename, but for now, I can't even get it to delete the file when hard-coding all the different possible paths I can think of.

Is there a way to test what's going on in this? Like an echo or... anything?

Any help or direction is greatly appreciated.


App::uses('File', 'Utility');

// new File(string $path, boolean $create = false, integer $mode = 493) 
// $mode  = 493 default

$file = new File(WWW_ROOT . 'img/uploads/rest_logo.jpg', false, 0777);
if($file->delete()) {
   echo 'image deleted.....';
}

Detail about File Utility


I tried deleting a file in afterDelete() and didn't find any issues..It worked for me..

    function afterDelete() {
        if(unlink(WWW_ROOT."/img/uploads/2.png")) {
            echo "Successful";
        }
        else echo "Unsuccessful";
}

I don't know what is the problem with your code...Maybe your problem is how to delete the same file after its record has been deleted. For that you can use the beforeDelete() function to store the image name in a certain variable and then use it in afterDelete().


This is an afterDelete I use and that works perfectly on my project, try it and see:

public function afterDelete() {
    $file = new File($this->data['MyModel']['file_path']);
    $file->delete();
}


I assume you can't use debug or a normal echo print_r($val,1);. You could mail yourself the results of this code with the php mail() function:

$perms = fileperms('/the/path/the_file.ext');

It should be something like 0755.

You could also echo all of this and place a die('die!'); at the end of the function and look what happens. Either way, if you don't get a email or the executions stops, something is wrong.

By the way, this is probably the right path: unlink(WWW_ROOT . 'img/uploads/rest_logo.jpg');


I'm going to give you my solution. At the end you can see I use the unlink() function and I can assure you that it works.

I use a this function inside a Component with another set of ImageFunctions

function getImageAbsolutePath( $upload_id ){
    //To use the model in the Component
    $this->Upload = ClassRegistry::init('Upload');

    $options = array(
        'conditions' => array(
            'Upload.id' => $upload_id
        )
    );

    $upload = $this->Upload->find( 'first', $options );

    if( isset( $upload['Upload'] ) && $upload['Upload']['id'] > 0 ){
        $imgPath = WWW_ROOT . "files" . DS . "uploads" . DS . $upload['Upload']['name'];
    }else{
        $imgPath = "";
    }

    return $imgPath;

}

In the controller

        $filePath = $uploadComponent->getImageAbsolutePath( $id );

        if( $filePath != '' && file_exists( $filePath ) ){
            if ($this->Upload->delete($id)) {
                unlink($filePath);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜