I am having some trouble with an image upload on my website. I have an upload page that lets people upload entries into a handbook. But when I upload an image, the $_FILES array is empty. I have:
- Checked that upload is on
- Adjusted maximum size allowed to 20M
- Added
enctype="multipart/form-data"
- Checked the size of the image (it is 38KB)
- Made sure I used the name attribute
- Changed permissions of temp folder to
777
- Made sure I used
POST
- Checked that I have enough space available (20GB is available)
- Other things that can be seen in the code
- I adjusted the code, and submitting without an image fills $_FILES with empty values as expected.
I have uploaded the output of phpinfo() on my server here.
Nothing has helped, wherever I looked. Note that I am running this on my local machine, a Mac (10.11.4) with Apache 2.4.18 and PHP 5.5.31.
I used the answer to Why would $_FILES be empty when uploading files to PHP? to check these things.
Here is the code:
Upload page (upload.php):
<?php include "sql.inc"; ?>
<html>
<head>
<title>The Emerson Nature Center Official Handbook | Upload</title>
<link rel="stylesheet" href="page.css" type="text/css">
<iframe src="navigator.php"></iframe>
<style>
textarea#styled {
width: 600px;
height: 120px;
border: 3px solid #cccccc;
padding: 5px;
}
</style>
</head>
<body>
<h1>Upload an entry to the Handbook!</h1>
<center>
<form action="upload_page.php" method="POST" enctype="multipart/form-data">
<h2>Title: <input type="text" name="title" placeholder="Title" style="font-size: 18pt;" required></h1>
<input type="hidden" name="author" value="<?php if ($canadd) echo $db->querySingle("SELECT name FROM passwords WHERE id=".$_COOKIE['userid']); ?>">
<h3 style="entry">Author: <?php if ($canadd) echo $db->querySingle("SELECT name FROM passwords WHERE id=".$_COOKIE['userid']); ?>
<textarea id="styled" width="300" height="300" placeholder="text" name="entry" required></textarea>
<br>
<h3>Insert images here: </h3>
Image 1 (required): <input type="file" name="image1" accept=".png, image/png" required><br>
Image 2 (optional): <input type="file" name="image2" accept=".png, image/png"><br>
<input type="submit" value="submit">
</form>
<br>
</center>
</body>
</html>
Upload handler (upload_page.php):
<?php
include "sql.inc";
$target_dir = "images/";
$images = scandir($target_dir);
$last_id = ltrim(end($images), "id-");
$last_id = rtrim($last_id, ".png");
$firstid = intval($last_id) + 1;
$target_file = $target_dir . "id-" . $firstid . ".png";
$uploadOk = 1;
$typeAllowed = array("image/png");
echo 'Array:<br>';
var_dump($_FILES);
//$check = getimagesize($_FILES["image1"]["tmp_name"]);
//if ($check !== false) {
// $uploadOk = 1;
//} else {
// echo "The first file you uploaded is not a valid PNG image.";
// $uploadOk = 0;
//}
if ($_FILES["image1"]["size"] > 500000) {
echo "Sorry, your first file is too large. Your size was " . $_FILES["image1"]["size"] . " bytes.";
$uploadOk = 0;
}
if (exif_imagetype($_FILES['image1']['tmp_name']) != IMAGETYPE_PNG){
echo 'You can only upload PNG images. (first file) Your type was "' . $_FILES['image1']['type'] . '".';
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your first file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image1"]["tmp_name"], $target_file)) {
$uploadOk = 1;
} else {
echo "Sorry, there was an error uploading your first file.";
}
}
if (isset($_FILES['image2'])) {
$target_dir = "images/";
$secondid = $firstid + 1;
$target_file = $target_dir . "id-" . $secondid . ".png";
$uploadOk = 1;
//$check = getimagesize($_FILES["image2"]["tmp_name"]);
//if ($check !== false) {
//} else {
// echo "The second file you uploaded is not a valid PNG image.";
// $uploadOk = 0;
//}
if ($_FILES["image2"]["size"] > 500000) {
echo "Sorry, your second file is too large. Your size was " . $_FILES["image2"]["size"] . " bytes.";
$uploadOk = 0;
}
if (exif_imagetype($_FILED['image2']['tmp_name']) != IMAGETYPE_PNG){
echo 'You can only upload PNG images. (second file) Your type was ' . $_FILES['image2']['type'];
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your second file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["image2"]["tmp_name"], $target_file)) {
$uploadOk = 1;
} else {
echo "Sorry, there was an error uploading your second file.";
}
}
}
if (isset($secondid)) $secondid = ",".$secondid;
else $secondid = "";
if ($uploadOk == 1) $db->exec('INSERT INTO handbook (id,title,author,entry,imageids) VALUES (NULL,"'.$_POST["title"].'","'.$_POST["author"].'","'.$_POST["entry"].'","'.$first id.','.$secondid.'")');
else echo "Your entry was not added.";
header(""); //to send headers to stop redirect
flush(); // ---^
if (!headers_sent()) header("Location: view.php?id=".$db->querySingle("SELECT id FROM handbook WHERE title='".$_POST['title']."'"));
else echo 'Please press the back button on your browser.';
?>
<html>
<head>
<title>Uploading...</title>
</head>
</html>
If you need extra code, logs, or php.ini entries, please tell me, and I will get the code necessary.