Download the contents of a file uploaded by a user before saving

See, I give users of my website the ability to upload their own PHP / HTML / TXT files to their server, right? ... But I want to get this content without saving it on my server and save the contents to a string or what something else.

Can this be done? ... If not, what should I do to get the contents after the file is saved on my server? ... please help me!

I don't know if this can help, but this is the code that I use to upload custom files.

$allowedExts = array("php", "html", "txt"); $tmp = explode(".", $_FILES["file"]["name"]); $extension = end($tmp); if ((($_FILES["file"]["type"] == "application/octet-stream") || ($_FILES["file"]["type"] == "text/php") || ($_FILES["file"]["type"] == "text/html") || ($_FILES["file"]["type"] == "text/plain")) && ($_FILES["file"]["size"] < 50000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } 
+6
source share
2 answers

Use file_get_contents() for this:

 echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; // store file content as a string in $str $str = file_get_contents($_FILES["file"]["tmp_name"]); 

Please note that if you need a file for later use, you will have to copy it to the destination additionally. Like this:

  move_uploaded_file($_FILES["file"]["tmp_name"], 'contents/' . $_FILE['name']); 
+11
source

Yes Maybe with file_get_contents

 $fileContent = file_get_contents($_FILES['upload_file']['tmp_name']); 

Below will be useful link below

php: file_get_contents removes php code

0
source

Source: https://habr.com/ru/post/946371/


All Articles