Why does my Php Image upload not work?

592

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

663

Answer

Solution:

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>
477

Answer

Solution:

Save the image using absolute path on the server

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

Answer

Solution:

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;

People are also looking for solutions to the problem: php - How to retrieve a route pattern from a route name in a Symfony Controller

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.