Reputation: 9
Newbie question. I'm trying to get attached files in PHP, but I get only an empty $_FILES superglobal array.
Here's my test code:
<!DOCTYPE html>
<html>
<head>
<title>Test file upload page</title>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$data = $_POST['text'];
$file = isset($_FILES['file']) ? $_FILES['file'] : NULL;
echo("<p>" . $data . "</p>");
echo(print_r($_POST));
echo(print_r($_FILES));
if ($file != NULL) {
echo("<h1>There is an attachment.</h1>");
$fileContent = file_get_contents($_FILES['file']['tmp_name']);
echo("<p>" . $fileContent . "<p>");
} else {
echo("<h1>There is no attachment.</h1>");
}
} else {
?>
<form action="formdata.php" method="post">
<textarea id="text" name="text" rows="4" cols="66" maxlength="300"></textarea>
<input id="file" type="file" name="file" onchange="" />
<!--button id="loadFile" onclick="">Upload</button-->
<input type="submit" value="Submit">
</form>
<?php
}
?>
And here's all I get, weather I attach a file to input gadget or not.
Test text here.
Array ( [text] => Prova [file] => Meow_Meow.jpeg ) 1Array ( ) 1
There is no attachment.
I have briefly checked php.ini configuration, which I have not edited and has default Ubuntu values. File uploads are definitely on, so I don't think it may be a configuration error.
(base) dario@devmachine:~$ grep -Ri "file_uploads" /etc
/etc/php/8.3/cli/php.ini:; Default Value: -1 (Sum of max_input_vars and max_file_uploads)
/etc/php/8.3/cli/php.ini:file_uploads = On
/etc/php/8.3/cli/php.ini:max_file_uploads = 20
/etc/php/8.3/apache2/php.ini:; Default Value: -1 (Sum of max_input_vars and max_file_uploads)
/etc/php/8.3/apache2/php.ini:file_uploads = On
/etc/php/8.3/apache2/php.ini:max_file_uploads = 20
My final object is to understand how to pass file attachments from JS to PHP. But before being able to do that, I would like to understand how to get attachments from PHP to PHP too!
Upvotes: 0
Views: 29