duanmu0834 2015-10-28 15:43
浏览 67
已采纳

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

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

  • 写回答

3条回答 默认 最新

  • duandangqin0559 2015-10-28 16:52
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c