Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preserve file name and extension using post request in php?

Tags:

post

php

Sorry if the title is worded badly, but I'm very new to this, and still learning. I spent about an hour googling and couldn't find the answer, so I figured I'd post my issue. I have a web server with a php script on it, and I'm trying to be able to send a post request from powershell that uploads a file, and the file will be saved to the web server. Here is the script I have right now:

<?php
$file = date("Hism") . ".txt";
file_put_contents($file, file_get_contents("php://input"));
?>

This works, but it always sets the file extension to txt, and the file name to the date. I found some things that I didn't fully understand about $_FILES["file"]["name"] but replacing the date("Hism") . ".txt" part with that gives the error:

PHP Notice:  Undefined index: file in /home/ubuntu/phpServer/i.php on line 2
PHP Notice:  Trying to access array offset on value of type null in /home/ubuntu/phpServer/i.php on line 2
PHP Warning:  file_put_contents(): Filename cannot be empty in /home/ubuntu/phpServer/i.php on line 3

If it matters, the PowerShell script I am using to send the post request is:

iwr $ip/i.php -method POST -infile C:\test.txt
like image 859
Fun840 Avatar asked Dec 07 '25 03:12

Fun840


1 Answers

I see that answer is a little vague, so Here is the complete answer. For your i.php

<?php

// Outputs the files variable array
// var_dump($_FILES);
if (empty($_FILES)) return '$_FILES is empty';

// Why file? and name? please note that if you perform var_dump($_FILES) as stated above you will get
// the array that makes up $_FILES request.
// Also note in our request:  name=`"file`"; filename=`"test.txt`"" Try to change and then see what var_dump dispalys.
$fileName = $_FILES['file']['name'];

// Note that tmp files are saved elese where check using var_dump($_FILES)
$fileTmpName = $_FILES['file']['tmp_name'];

// Now move to the current directory where the script is running from
move_uploaded_file($fileTmpName, $fileName);
?>

That script should handle the preservation of extension and name of file. Sources for above script and since your new please have a look at:

  • https://www.w3schools.com/php/php_file_upload.asp
  • https://www.tutorialspoint.com/php/php_file_uploading.htm and
  • https://www.php.net for more in depth on what $_FILES and move_upload_file do.

Lets now move to iwr. Your are using Invoke-WebRequest do help iwr for more info. Now this question has been answered many times but here goes: (sources below)

$Uri = 'http://my-ip/Stackoverflow/i.php'
$fileBytes = [System.IO.File]::ReadAllBytes('c:\test.txt');
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString();

$LF = "`r`n";

$bodyLines = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"test.txt`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF" 
) -join $LF

The above script code is from powershell invoke-restmethod multipart/form-data I recommend that you look at that as well.

iwr -Uri $Uri -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines

Note I am using iwr the source mentioned above uses irm. You can read more on help iwr or help irm or visit Invoke-WebRequest

like image 86
ziaahsan Avatar answered Dec 08 '25 17:12

ziaahsan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!