File selected but missing in PHP? Trace a CSV upload
When you see a file name, your browser has selected the file. To see whether it reached PHP, check what the browser sent and what PHP sent back. Use one small CSV to find out where an upload can stop.
Make a form that sends a CSV
Try this on a computer with PHP 8. Run php -v in a terminal to see your version. Make a
practice folder and save the two examples below as index.html and receive.php,
both in UTF-8. You will run the upload on your own computer.
A CSV is a text file that uses commas to separate values. The first line in this sample names the columns. The next lines list things and how many of each there are.
See what the sample CSV contains
item,quantity
Pencil,2
Notebook,1
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<title>Send a CSV</title>
</head>
<body>
<h1>Send a CSV</h1>
<form action="receive.php" method="post"
enctype="multipart/form-data">
<label for="csv-file">CSV to send</label>
<input id="csv-file" name="csv" type="file"
accept=".csv,text/csv" required>
<button type="submit">Send</button>
</form>
</body>
</html>
action- This tells the browser where to send the file. Here,
receive.phpis in the same folder as the form. method / enctypeposttells the browser how to send the form.multipart/form-datalets the request carry the contents of the file.name="csv"- This names the field in data sent to PHP. The
idconnects the label to the file input. accept / requiredaccepthelps the file picker suggest CSV files.requiredtells the browser to ask for a file if you try to send the form without one.
Receive the CSV in PHP
PHP puts details about uploaded files in $_FILES. The form names the field csv,
so look in $_FILES['csv']. If its error value is UPLOAD_ERR_OK,
PHP received the file. Then the code checks the size and reads the CSV.
This example reads a CSV of up to 1,024 bytes. It checks for item,quantity on the first line
and counts rows with an item name and a positive whole number. The sample uses UTF-8 without an extra
marker at the start of the file (a BOM). PHP puts the upload in a temporary file and cleans it up when
the request ends.
Show the receive.php code
receive.php
<?php
function reply(int $status, string $message): void
{
http_response_code($status);
header('Content-Type: text/plain; charset=UTF-8');
exit($message . "\n");
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Allow: POST');
reply(405, 'Send a CSV from the form.');
}
$file = $_FILES['csv'] ?? null;
$error = $file['error'] ?? null;
if (!is_int($error)) {
reply(400, 'PHP could not find the csv field. Check the form and request size.');
}
if ($error === UPLOAD_ERR_NO_FILE) {
reply(400, 'Choose a CSV.');
}
if ($error === UPLOAD_ERR_INI_SIZE) {
reply(413, 'The file is larger than the PHP limit for one file.');
}
if ($error !== UPLOAD_ERR_OK) {
reply(400, "PHP could not receive the file. Error $error.");
}
if ($file['size'] > 1024) {
reply(413, 'This example accepts CSV files up to 1,024 bytes.');
}
$stream = fopen($file['tmp_name'], 'rb');
if ($stream === false) {
reply(500, 'The uploaded file could not be read.');
}
$header = fgetcsv($stream, 0, ',', '"', '');
if ($header !== ['item', 'quantity']) {
reply(422, 'The first line must be item,quantity.');
}
$rows = 0;
while (($row = fgetcsv($stream, 0, ',', '"', '')) !== false) {
if (count($row) !== 2 || trim($row[0]) === '' ||
!preg_match('/\A[1-9][0-9]*\z/', $row[1])) {
reply(422, 'Each row needs an item name and a positive whole number.');
}
$rows++;
}
if (!feof($stream)) {
reply(500, 'The uploaded file could not be read.');
}
fclose($stream);
if ($rows === 0) {
reply(422, 'Add at least one item row.');
}
reply(200, "CSV received. Data rows: $rows.");
See what was sent and what came back
-
Open a terminal in the folder with the two files. Run the command below to start a PHP server on
your own computer.
Terminal
php -d file_uploads=1 -d upload_max_filesize=2K -d post_max_size=4K -S 127.0.0.1:8000 - Open
http://127.0.0.1:8000/in your browser. Open the Network tab in the developer tools. Choose the sample CSV and select Send. - Select
receive.phpin the list. Under Headers, check that the request method isPOST. Under Payload, look for thecsvfield in the form data. - Open Response to read the reply from PHP. With the sample CSV, you will see status
200and the reply below. When you finish, press Ctrl + C in the terminal.
Reply from PHP
CSV received. Data rows: 2.
Find out where it stopped
Network shows no POST request
Check that you chose a file. This form has required. If you select Send with no file,
the browser asks you to choose one and stops the request.
There is a POST request, but it has no csv field
Remove name="csv" and try the same file again. You can still see its name in the file
picker. The sent data has no csv field, so PHP replies with 400. Give the
input the same name that PHP looks for.
There is a POST request, but PHP has no file details
Check action for the address and enctype for the form data format. If the
problem only happens with larger files, check the size limits below.
PHP replies with 422
PHP received the file. Read the message in Response to see what to fix in the CSV. For example,
change item to name on the first line. PHP will reply, “The first line
must be item,quantity.”
Three size limits to check
1,024 bytes- The largest CSV this code accepts. PHP can receive a larger file and the code will reply with
413. 2 KB- The startup command sets
upload_max_filesizeto this value. This limits one file. A larger file givesUPLOAD_ERR_INI_SIZE. 4 KB- The startup command sets
post_max_sizeto this value. This limits the entire request body, including the file and the form data around it. Over that limit,$_FILESand$_POSTare empty. This example replies with400, so check the request size as well as the status.
To compare the limits, repeat the sample data rows to make files of about 1.5 KB, 3 KB, and 6 KB. They hit the code limit, the one-file limit, and the request limit, in that order. For the 3 KB file, check that the entire request body stays below 4 KB.
Check your understanding
You can see the file name and find the POST request. The Payload tab has no csv field. What
would you check first?
Show the answer
Look for name="csv" on the file input. Seeing a file name tells you the browser selected
a file. The input needs a name for the browser to include the field in the request.