我在 php 中在哪里放置未设置和取消链接


Where do I place unset and unlink in php?

我有一些php代码。我希望在完成打印后取消设置主题并取消链接$outputfile。

$file = "files/textfile.txt" ;
$processed = "processed.txt" ;
$outputfile = "files/outputfile.txt" ;
if(!empty($_POST)){
    writetofile($file, $text, "w");
    preproc($file);
    execpython($processed);
    $topic = getoutput($outputfile);
}

因此,我将其打印在底部的某个位置:

echo '<span style="color:red;">' . $topic . '</span>';

不确定我应该如何放置unlinkunset因为它们似乎提前删除了输出文件,然后我没有收到任何输出。我需要删除它,因为它有时会被覆盖并产生相同的输出。

您可以使用 getoutput() 从文件读取文件后删除该文件,也可以在打印完后取消设置变量。

$file = "files/textfile.txt" ;
$processed = "processed.txt" ;
$outputfile = "files/outputfile.txt" ;
if(!empty($_POST)){
    writetofile($file, $text, "w");
    preproc($file);
    execpython($processed);
    $topic = getoutput($outputfile);
    unlink($outputfile);
}
echo '<span style="color:red;">' . $topic . '</span>';
unset($topic);