3


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.

Community
  • 1
  • 1
JackMacWindows
  • 377
  • 5
  • 15
  • I execute your form and there is no problem in the HTML form, it is posting file and i am receiving both the files in $_FILES. You need to check some more configuration post_max_size etc and try to look for the configuration by running phpinfo. – Afshan Shujat May 13 '16 at 02:57
  • `!$canadd = $db->querySingle` what is that? – Asur May 13 '16 at 08:59
  • @asurbernardo That is for the project in whole (basically so only certain accounts can use the page), so I'll remove it here. – JackMacWindows May 13 '16 at 14:27
  • @JackMacWindows You are not comparing there, there is not a comparing operator, all I can see is an assignment – Asur May 13 '16 at 14:32
  • @asurbernardo In the SQLite database, `canadd` is a boolean value, and if canadd is false (`!$canadd`) it will move on. I assign the value so I can use the value later without sending another query. – JackMacWindows May 13 '16 at 14:39
  • Wow that's soo counterintuitive, but I see... it doesn't look like a good practice btw. – Asur May 13 '16 at 14:41

0 Answers0