Hi All,

I'm trying to unlink a file.

I start with a form and submit it using javascript's submit() (out of necessity). Eventually the php code arrives at a function and unlinks the file (or rather doesn't).

Now, the files unlinks when I use die() to exit out of script at the end of the function when testing but when I run it normally, nothing gets unlinked and the file remains. The code reaches the unlink() function either way.

Is this a javascript problem or php issue?

Thanks for any help offered.

So it's not the die() part.

When I submit it using the javascript submit() it doesn't unlink, but if I refresh the page and resend the post variables it works. So I assume it must be something caused by the JS.

Well, I can't see how it can be a JS problem either as the only value being sent through is a POST value which appears either way.

My unlinking code is:

public static function deleteEmail($id){
        if(!isset($_SESSION['agencyId'])||empty($_SESSION['agencyId'])){
            // throw error
        }

        $q = "DELETE FROM recruitment_email WHERE id=:id";
        $stmt = \Animus\Animus::app()->dbc()->prepare($q);
        $stmt->bindParam(':id',$id,PDO::PARAM_INT);
        if($stmt->execute()){
            $emailDir = Animus\Animus::app()->getBasePath().DIRECTORY_SEPARATOR.'emails'.DIRECTORY_SEPARATOR.$_SESSION['agencyId'].DIRECTORY_SEPARATOR;
            if(is_dir($emailDir)){
                if($files = scandir($emailDir)){
                    $attachments=self::getAttachmentsForEmail($id);
                    foreach($attachments as $attachment){
                        if(in_array($attachment,$files)&&is_file($emailDir.$attachment)){
                            unlink($emailDir.$attachment);
                        }
                    }
                }
            }
        }
    }

So, I can't see why it's not working the first time it runs through it but it unlinks when I refresh the page... :S

Ok. So, when I run the code using the form submit, the file is found and I presume, unlink() runs or begins to or something. However, the file is not deleted. When I run the code again using refresh or by resubmitting the form, the file is not found, but the file is then deleted.

So to summarise:
When the file is found it is not deleted.
When the file is not found it is deleted.

Anyone have any ideas as to what might be happening?

Member Avatar

Just a suggestion - have a play with it, formulate your question, then, and only then, ask. You've posted 5 successive posts. Are we to wait for another few? Have you passed on the entire info? Can we start contributing?

I've been sitting in front of it all day testing and nothing makes sense. I'm trying to add anything to the post that I find out so that it might become apparent to someone what the problem may be. I could spend a week trying to figure it out and not ask for any help until the weekend and the solution may have been obvious to someone way beforehand. I'd rather not spend a week on this :)

So in answer to your question. I've passed on as much information as I believe could help resolve the problem and yes, you or anyone else may contribute at any time.

Member Avatar

The point is Tinnin, posting a running commentary once your outlined the question isn't necessarily going to help. If a contributor is going to set any time aside to help you, then he or she needs to be aware of the problem in full right in the first post. No added bits or experimental bits or changes - those are bound to infuriate. Let the contributions come. Ok, hope we get some bites then. Busy at the mo, but if nobody takes the hook, I'll be back later. :)

Ok. Noted. Thanks Diafol :)

Member Avatar

I'm afraid I can't replicate your code as you're using static calls to methods that I don't have but:

You're deleting records from the DB BEFORE they are unlinked - you can't be sure that they've been unlinked, so you shouldn't do that.

I suggest that you place some echo statments throuout your code and test the output at each step, e.g.

public static function deleteEmail($id){
    if(!isset($_SESSION['agencyId'])||empty($_SESSION['agencyId'])){
        // throw error
    }
    $emailDir = Animus\Animus::app()->getBasePath().DIRECTORY_SEPARATOR.'emails'.DIRECTORY_SEPARATOR.$_SESSION['agencyId'].DIRECTORY_SEPARATOR;
    if(is_dir($emailDir)){
        if($files = scandir($emailDir)){
            $attachments=self::getAttachmentsForEmail($id);
            if(!empty($attachments)){
                $count = 0;
                foreach($attachments as $attachment){
                    if(in_array($attachment,$files) && is_file($emailDir.$attachment)){
                        if(unlink($emailDir.$attachment)){
                            echo "File ($attachment) unlinked<br />";
                            $count++;                               
                        }else{
                            echo "File ($attachment) not unlinked<br />";
                        }
                    }else{
                        echo "$attachment not in filelist or not a file<br />";  
                    }
                }
                if($count > 0)
                {
                    //Do DB removal here

                }
            }else{
                echo "No attachments<br />"; 
            }
        }else{
            echo "No files found<br />"; 
        }
    }else{
        echo "$emailDir not a directory<br />";  
    }
}

That's just for simple error display - get rid of it once you've identified the problem.

All fixed now.

I found a post with a similar problem here
Click Here

I added the chmod($emailDir.$attachment, 0777); line before unlink($emailDir.$attachment) and now it is working.

Doesn't make sense to me as no permissions warning was produced without the chmod and unlink() returned true and the file was deleted eventually but with a massive delay. Now it happens instantaneously.

Windows/WAMP issue? I don't know.