Creating PHP file to check if other file exists (from PHP file)

Yes, I know my title is a bit confusing. Anyway, I have a PHP file, which I want to drop another PHP file, and the dropped PHP file would check if the creator exists, and if it doesn't it deletes files, how could I do this? I know how to drop a text file with some content in it, but not a much more complicated PHP file with multiple lines.

Is there anyway to do this?

Upvotes: 0

Views: 46

Answers (1)

Parziphal
Parziphal

Reputation: 6902

Assuming that with "drop" you mean "create", this would work:

File parent.php:

<?php

// Child destination
$childFile = __DIR__ . '/child.php';

// Full path to this parent file
$file      = __FILE__;

// Child contents
$contents = <<<EOT
<?php

if (!is_file('${file}')) {
    // Parent doesn't exist; delete some files and exit
    // unlink(...);
    exit;
}

// Parent exists; keep doing something else
echo "I'm the child!";

EOT;

// Create child file
file_put_contents($childFile, $contents);

// Do something else...
echo "Created child";

The parent creates a child file, where the existance of the parent is checked; if it doesn't exist, the child will do something else and then exit.

Upvotes: 1

Related Questions