0

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");
   }
}
  • 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
1

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");
}
  • 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
  • @noeltock if you unset a variable, it cannot be used just after with unlink then the 2nd code cannot work – mmm Feb 22 '18 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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