Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can somebody tell me why this isn't working? I'm trying to get it to create a file in the plugins directory. After I get this working, I'll be trying to get this to create a file in the active theme's directory. Can somebody help me out here?

EDIT

I've changed my code, right now it creates the file but it does not input the text 'Testing' into the file even. Is there something I'm missing here?

$filename = __FILE__;

register_activation_hook($filename, 'superActivation');
register_deactivation_hook($filename, 'superDeactivation');

global $myFile, $fh, $stringData, $filename, $pluginbase, $full_plugindir;

$myFile = "../wp-content/themes/striking/testFile.php";
$stringData = "Testing\n";
$fh = fopen($myFile, 'w') or die("can't open file");

function superActivation() {
    global $myFile; global $fh; global $stringData; global $filename;
    fwrite($fh, 'test');
    fclose($fh);
}

function superDeactivation() {
   global $myFile;
   if(is_file("$myFile")) {
   unlink("$myFile");
   }
}
share|improve this question
1  
Be more explicit about "not working". Is the activation function not run at all, or is the function run but the file isn't created? –  MathSmath Jan 10 '11 at 16:57
    
@MathSmath I got it to create the file now, and in the right directory, but now I'm just having a problem with my deactivation hook. It won't delete the file, it says I don't have permission. Any ideas? –  Jared Jan 10 '11 at 20:24
    
@Jared What are the testFile.txt's permissions? You can use chmod to set permissions on a file right after you create it (see php.net/manual/en/function.chmod.php). It could also be an ownership issue. If the file isn't properly owned, it won't matter what the permissions are. Assuming you're running apache, I find that CHOWNing all my uploaded files to apache:apache usually works for me, as apache is the user that php runs under. But hopefully someone with more premissions/ownership experience can chime in... I only know enough to be dangerous :) –  MathSmath Jan 10 '11 at 21:12
    
@MathSmath - the thing is, this is for a client so it needs to all be automated. The project I'm working on started out as a theme, and now I'm making it into a plugin. There's already tons of people who use the theme, which is why I need it all to be automated. Is there any way to do this without having to CHMOD or CHOWN? –  Jared Jan 10 '11 at 21:26
    
@Jared It can be automated. chmod and chown are both php functions, and you can use 'em right from your function. Check the link I included. But before you do that, I'd try CHMODding and CHOWNing manually to see if that in fact fixes your problem. If it does, just include a chmod/chown step right after your file upload. Know what I mean? –  MathSmath Jan 10 '11 at 23:09
show 3 more comments

1 Answer

up vote 0 down vote accepted

For unlinking, try:

function superDeactivation() {
   $myFile = "testFile.txt";
   if(is_file("$myFile")) {
   unlink("$myFile");
   }
}

Or alternatively:

function superDeactivation() {
   $myFile = "testFile.txt";
   unset($myFile);
   unlink("$myFile");
}
share|improve this answer
    
I tried both, but with no luck. The second code, if I remove unlink("$myFile"); and just leave unset($myFile); there, it doesn't give me any errors anymore but it still doesn't delete the file. I'm also having problems getting the php to input data to the file. –  Jared Jan 10 '11 at 21:23
    
I got this to work and thanks for leading me in the right direction. –  Jared Jan 12 '11 at 6:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.