为什么我的Php图片上传不起作用?

时间:2015-10-28 15:43:35

标签: php file-upload

我尝试让用户上传图片,但它不起作用。没有错误或任何内容,但文件没有出现在文件夹中。

$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.";
       }
   }
}

用户请求的html表单:

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

欢迎任何帮助:D

3 个答案:

答案 0 :(得分:1)

希望以下内容可以帮助您实现这一目标 - 它可以在我的测试系统上上传两个文件

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

答案 1 :(得分:0)

看起来你从未将$ uploadOk设置为0以外的其他任何内容。在执行if($ uploadOk == 0)检查之前,您需要将其设置为其他内容。我建议在第一个if语句之外的顶部附近添加:

$uploadOk = 1;

答案 2 :(得分:0)

使用服务器上的绝对路径保存图像

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