运行 PHP 文件上传时

Run PHP file when uploaded

我在本地服务器上有一个简单的 php 网站,它只是将任何类型的文件上传到服务器(我本地电脑上的一个目录)上。

我想要的是当我上传一个php文件时,我希望它执行,而不只是保存它。 没有安全,我只是想看看能不能在上传的时候执行一个php脚本。 我怎样才能做到这一点?

您可以将文件写入磁盘,然后再需要它。如果您永久不需要该文件,那么我建议使用 temp file.

我建议然后使用 require_once 来执行文件。

类似于(为简单起见,我使用硬编码文件名):

<?php

// Logic for handling the file upload goes here

// Demo script to run, this should be the contents of the file uploaded.
$upload_contents = '<?php echo "I have been uploaded."; ?>';

// Write the file to disk, I've used a hard-coded name and contents to serve the purpose of an example
file_put_contents('UploadedScript.php', $upload_contents);

// Since security is not a requirement, we will just straight require the file so PHP will execute it.
require_once('UploadedScript.php');

?>

编辑:如果您想上传任何类型的文件,但只执行扩展名为“.php”的文件,那么我建议查看 these answers。然后你可以在执行之前检查确保上传的文件是“.php”扩展名。