0

I try to let the user upload an Image, but it doesn't work. No Error or anything, but the file doesnt appear in the folder.

$id = $rowcount + 1;
$pfad_imgfront = "bilder/front/";
$pfad_imgback = "bilder/back/";

//Fileupload
if ($_FILES['imgfront']['size'] > 0) {
    $pfad_imgfront = $pfad_imgfront . basename($_FILES['imgfront']['name']);
    $dateityp = pathinfo($pfad_imgfront, PATHINFO_EXTENSION);

    if ($_FILES["imgfront"]["size"] > 2000000) {
        echo "Ihr Bild ist grösser als 2MB.";
        $uploadOk = 0;
    }

    if ($dateityp != "jpg" && $dateityp != "png" && $dateityp != "jpeg" && $dateityp != "gif" && $dateityp != "bmp") {

        echo "Nur JPG, JPEG, PNG, BMP & GIF Dateien sind erlaubt.";
        $uploadOk = 0;
   }

   if ($uploadOk == 0) {
       echo "Sorry, your file was not uploaded.";
       // if everything is ok, try to upload file
   } else {
       if (move_uploaded_file($_FILES["imgfront"]["tmp_name"], $pfad_imgfront))   {
           rename($pfad_imgfront, $id . 'front');

       } else {

           echo "Sorry, there was an error uploading your file.";
       }
   }
}

The html Form that was requested by a user:

 <form enctype="multipart/form-data" action="neu_aufgabe.php"
 method="POST">
     <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
     Bild zur Aufgabe (max. 2MB): <input name="imgfront" type="file" /><br />
     <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
     Bild zur Lösung (max. 2MB): <input name="imgback" type="file" /><br />
     <input type="submit" value="Senden" /> </form>

Any help is welcomed :D

12
  • can you check the value of $uploadOk before if ($uploadOk == 0) { and let us know Commented Oct 28, 2015 at 15:48
  • Can you post the form you use Commented Oct 28, 2015 at 15:51
  • this looks like an example from w3schools if I remember right....weird.. Commented Oct 28, 2015 at 15:52
  • @LuckyChingi nothing shows up..... So it seems it doesnt even go through the first if (size > 0)... Commented Oct 28, 2015 at 15:55
  • @Andrew yes i copied some of their code... Commented Oct 28, 2015 at 15:55

3 Answers 3

1

Hopefully the following will help you get this working - it uploads both files fine on my test system

<?php

        $id=$rowcount+1;
        $root = realpath( $_SERVER['DOCUMENT_ROOT'] );

        foreach( $_FILES as $field => $arr ){

            $errors=array();

            $size=$_FILES[ $field ]['size'];

            if( $size > 0 ){

                $filename = $_FILES[ $field ]['name'][0];

                switch( $field ){
                    case 'imgfront': $path=realpath( $root . '/bilder/front/' ); $newname=$path . DIRECTORY_SEPARATOR . $id . 'front' . $filename; break;
                    case 'imgback': $path=realpath( $root . '/bilder/back/' ); $newname=$path . DIRECTORY_SEPARATOR . $id . 'back' . $filename; break;  
                }               

                $imgpath = $path . DIRECTORY_SEPARATOR . $filename;

                $ext = strtolower( pathinfo( $imgpath, PATHINFO_EXTENSION ) );



                if( isset( $_POST['MAX_FILE_SIZE'] ) && $size > $_POST['MAX_FILE_SIZE'] ){
                    $errors[]='File too large';
                }
                if( !in_array( $ext, array('jpg','png','gif','bmp') ) ){
                    $errors[]='File is wrong type';
                }

                if( !empty( $errors ) ){
                    print_r( $errors ); 
                } else {
                    $res=move_uploaded_file( $_FILES[ $field ]["tmp_name"][0], $imgpath );
                    if( $res ) rename( $imgpath, $newname );
                }
            }
        }   
?>


    <form enctype="multipart/form-data" action="/test/target.php" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
         <div>
            Bild zur Aufgabe (max. 2MB): <input name="imgfront[]" id='fr' type="file" />
         </div>
         <div>
             Bild zur Lösung (max. 2MB):  <input name="imgback[]" id='bk' type="file" />
         </div>
         <input type="submit" value="Senden" />
    </form>
Sign up to request clarification or add additional context in comments.

3 Comments

Seemes to work somehow, but now I get: 'Array ( [0] => File too large ) Array ( [0] => File too large ) '
it was all very quickly written so you will need to play around with it to get it to work 100%. Perhaps leave the display of errors until the outer loop has finished? Incidentally, 2Mb is more like 2097152 than 2000000
$size=$_FILES[ $field ]['size']; should be $size=$_FILES[ $field ]['size'][0];
0

Save the image using absolute path on the server

            $root = realpath($_SERVER["DOCUMENT_ROOT"]);
            $pfad_imgfront = $root ."/bilder/front/";
            $pfad_imgback = $root."/bilder/back/";

Comments

0

It looks like you never set $uploadOk to anything other than 0. You'll need to set it to something else before doing the if($uploadOk == 0) check. I suggested adding this near the top, outside of your first if statement:

$uploadOk = 1;

1 Comment

I thought of the same but then if $uploadOk is not defined then its definitely not '0'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.