3

i'm currently working on a small script for my Homepage but i ran into a problem.

I Try to upload an Image, but it seems like the POST data from the form is not being received. What did i do wrong?

I already changed the post_max_size and everything in the php.ini.

These are the Errors i get:

"Notice: Undefined index: image in ...." & "Notice: Undefined index: submit in ...."

 <form method="POST" action="/eye/sites/handling/post.php" enctype="multipart/form-data">
     <div class="fileUpload">
        <span><i class="fa fa-folder-o" aria-hidden="true"></i> Bild wählen</span>
    <input type="file" class="upload" name="image"/>
    </div>
    <input type="submit" value="Upload It!" name="submit"/>
 </form>

<?php session_start();
error_reporting(E_ALL);
if (isset($_SESSION["login_stat"])) {
date_default_timezone_set('Europe/Berlin');
$config = "$_SERVER[DOCUMENT_ROOT]/eye/more/config.xml";
$xml = simplexml_load_file($config);
$picWidth = $xml->pic->width;
$picHeight = $xml->pic->height;
$fulldate = date('dmYHis');
if(isset($_POST["submit"])) {
  if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
    $typeCheck = $_FILES['image']['type'];
    if ($typeCheck != "image/jpeg") {
      $error = "Not a .jpg";
      header('location: /eye/sites/post.php?stat=bad&error='.$error);
      exit;
    }
    $file = $_SERVER['DOCUMENT_ROOT']."/uploads/".$fulldate.".jpg";
    $type = "image/jpeg";
    move_uploaded_file($_FILES['image']['tmp_name'], $file);
    $file_thmb = $_SERVER['DOCUMENT_ROOT']."/uploads/!1A_thmb/".$fulldate.".jpg";
    include "resize-class.php";
    $resizeObj = new resize($file);
    $resizeObj->resizeImage($picWidth, $picHeight, 'crop');
    $resizeObj->saveImage($file_thmb, 100);
    // header('location: /eye/sites/post.php?stat=good');
  } else{
  // header('location: /eye/sites/post.php?stat=bad&error=No File');
  }
} else{
  // header('location: /eye/sites/post.php?stat=bad&error=No Data');

  echo  $_SERVER['CONTENT_TYPE'];
  echo "<br>";
  echo $_FILES['image']['tmp_name'];
  echo "<br>";
  echo $_POST['submit'];
  echo "<br>";

}
} else {
header('location: /eye/index.php?stat=in');
}
?>

Edit: The problem is definitely about my Localhost. This whole thing is working fine on my Webspace, but on my localhost it's not working. BUT: I'm not getting errors anymore, when is click on Submit it goes to the php file that should save the image, but nothing is happening. I just see a white Page. But like i said, it runs perfectly on my webspace..

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Nils.
  • 39
  • 3
  • Have you tried `var_dump($_POST);` to see what you get? – Zeke Nov 19 '17 at 02:01
  • @Zeke Tried it right now, it just shows me an empty array. – Nils. Nov 19 '17 at 02:05
  • Then it all makes sense, the error says it and it’s the real issue: post data isn’t being sent... but your code looks impecable. There must be some misconfiguration somewhere and we’re missing it... – Zeke Nov 19 '17 at 02:12
  • @Zeke I actully dont know where i should search for the problem because i didnt change any configuration and some days ago it just worked fine – Nils. Nov 19 '17 at 02:24
  • I actually have an idea but it requires JavaScript knowledge... if you’re any good at it, I can give you a hand... – Zeke Nov 19 '17 at 02:27
  • @Zeke I only have very basic knowledge of JavaScript, so this could bring some problems :D But: I Just tried to output the upload_max_filesize and the post_max_size and i realised that these values actully arent the ones i put in my php.ini, so it seems like it's ignoring the settings in my php.ini, but some days ago it did read them. Maybe thats connected somehow? – Nils. Nov 19 '17 at 02:44
  • Okay, that’s some good news, check if the file has the correct values (they could have been overwritten) and try restarting Apache. If that doesn’t work, use `ini_set()` to manually set the PHP config. – Zeke Nov 19 '17 at 02:47
  • Well, actually you can just try the latter first, just to make sure that’s the problem... that might save some time – Zeke Nov 19 '17 at 02:49
  • @Zeke I tried ini_set(), but the values are still wrong and its not working.. I feel like everything broke somehow :D – Nils. Nov 19 '17 at 03:08
  • Did you use `ini_set()` at the beginning of the code? You can also try `ini_get()` to see if the values are ok. Other than that... all I can think of is to create a JavaScript function that alerts the file name when clicking on a button that runs that function. But this is really strange. – Zeke Nov 19 '17 at 03:15
  • @Zeke Okay im pretty sure the Problem is my Local Server. I Uploaded it all to my webspace and its working without any problem there. I used USBWebserver to check if the problem would appear agein (Normaly i use a WAMP Server). Same problem with USBWebserver, so the hole problem seems to be local... I dont know why, i really dont know what happend – Nils. Nov 19 '17 at 08:13
  • `var_dump($_FILES);` is the first thing you want to do. If that contains anything non-zero under the `error` key, go check the manual on what the specific reason is. If it is totally empty, then start error-checking your HTML, validate it as a first measure. – CBroe Nov 19 '17 at 08:48
  • @CBroe error under var_dump($_FILES); is 0. – Nils. Nov 19 '17 at 14:13

2 Answers2

1

If this is running on your local machine, do a quick check to make sure your "php.ini" file is configured to allow file uploads.

php.ini

file_uploads = On

The codes look fine. Check if your form action is posting to the correct path and if I may suggest using a simpler approach to test your file upload function before making it more complex. Use the following to start testing.

if (isset($_POST["submit"])) {
    if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
        echo "Upload is working!";
    }
} 

Keep us updated on your findings.

Bill L.
  • 91
  • 5
  • I added it to my php.ini, but it's still not working. I used ur small test code, still not working. I fell like my local server isn't working, i never had problems with uploading files or sending POST data and now its not working. I tried restarting it and everything, but nothing works. Im confused Edit: Seems like its not my local server, other sites i have on there are working just fine. Now im even more confused – Nils. Nov 19 '17 at 01:57
  • I think file_uploads is already in php.ini somewhere. Shouldn't be adding this line. After changing php.ini, did you restart your Apache services ? – Bill L. Nov 19 '17 at 04:40
0

Perhaps this general information will help someone, as it helped me: a submitted form will only include fields that have defined 'name' attributes. 'id' is not enough.

The idea is that 'id' identifies an element in the DOM for use by JavaScript (either as a global variable or for use in document.getElementById(ID)), but 'name' identifies those elements whose names and values will be sent to the destination ('action') page.

So it makes sense that there are two different identifying attributes, used in two different ways.

David Spector
  • 1,520
  • 15
  • 21