-2

I have a simple php website on a local server which simply uploads a file of any type on the server (a directory on my local pc). image

What I want is that when I upload a php file, I want it to execute, not only save it. There is no security, I just want to see if it is possible to execute a php script when uploading. How can I achieve that?

  • Okay, so how would you get the code in a "normal" file to execute, if it was _not_ uploaded via a form ...? Just do the same thing with your uploaded file then. – CBroe Dec 06 '21 at 15:34
  • I would explicitly run it. What I want is some kind of injection which runs the uploaded php file without explicitly running it – yoyopi768 yoyopi768 Dec 06 '21 at 15:36
  • Looks like you are asking how the php execute works, maybe this can help you https://stackoverflow.com/questions/51961985/how-to-run-a-php-script-in-a-shell-on-my-website, in addition to invoke it either within php running in apache web it can also be started automatically, faster way is a bash triggered by crontab every second that can check if there is any new uploaded file and run it before renaming or moving do a "processed" folder or by creating a proper daemon/service program – A. Lion Dec 06 '21 at 15:37

1 Answers1

1

You can write the file to disk and then require it after. If you do not need the file permanently then I would suggest a temp file.

I suggest then using require_once to execute the file.

Something similar to (I am using a hard-coded file name for simplicity):

<?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');

?>

Edit: If you're wanting to upload files of any type, but only execute files with a ".php" extension, then I suggest looking at these answers. Then you can check to ensure the file uploaded is of ".php" extension before then executing.

Sutton
  • 300
  • 2
  • 14