dongwei2882 2013-02-01 01:10
浏览 40

我无法使用PHP上传多个图像[重复]

Possible Duplicate:
PHP - Upload multiple images

I want to upload multiple images using PHP and I am stuck. I have the code but it uploads only 1 image.

<?php
if (isset($_POST['newuser'])) {
    if ((!empty($_POST['year'])) AND (!empty($_POST['make'])) AND (!empty($_POST['model'])) AND (!empty($_POST['engine'])) AND (!empty($_POST['mileage'])) AND (!empty($_POST['exterior'])) AND (!empty($_POST['interior'])) AND (!empty($_POST['transmission'])) AND (!empty($_POST['body'])) AND (!empty($_POST['fuel'])) AND (!empty($_POST['drive'])) AND (!empty($_POST['doors'])) AND (!empty($_POST['description']))) {
        $year = htmlspecialchars($_POST['year']);
        $make = htmlspecialchars($_POST['make']);
        $model = htmlspecialchars($_POST['model']);
        $engine = htmlspecialchars($_POST['engine']);
        $mileage = htmlspecialchars($_POST['mileage']);
        $exterior = htmlspecialchars($_POST['exterior']);
        $interior = htmlspecialchars($_POST['interior']);
        $transmission = htmlspecialchars($_POST['transmission']);
        $body = htmlspecialchars($_POST['body']);
        $fuel = htmlspecialchars($_POST['fuel']);
        $drive = htmlspecialchars($_POST['drive']);
        $doors = htmlspecialchars($_POST['doors']);
        $description = htmlspecialchars($_POST['description']);

        $target = "images/default.jpg";
        $msg = "";        
        if (!empty($_FILES['fisier']['name'])) {    
            $target = "images/";
            $target = $target . basename($_FILES['fisier']['name']);
            $file_size = $_FILES['fisier']['size'];    
            $file_type = $_FILES['fisier']['type'];
            $ok = 1;
            if ($file_size > 2048000) {
                echo "Too large";
                $ok = 0;
            }
            if ($file_type == "application/octet-stream") {
                echo "no PHP";
                $ok = 0;
            }
            if ($ok == 0) {
                echo "No file saved";
            } else {
                if (!move_uploaded_file($_FILES['fisier']['tmp_name'],$target)) {
                    $target = "images/default.jpg";
                    $msg = "No file saved. ";
                }
            }
        }
        require_once("mysql_connect.php");
        $sql = "INSERT INTO astonmartin VALUES('','$year','$make','$model','$engine','$mileage','$exterior','$interior','$transmission','$body','$fuel','$drive','$doors','$description','$target','$target2','$target3','$target4','$target5','$target6')";
        mysql_query($sql) or die(mysql_error());
        $msg .= "";    
        header("Location: add_user.php?msg=$msg");
    } else {
        $error = "Complete form";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dongsheng4126335 2013-02-01 09:01
    关注

    I think you need first this function assoc, here: multidimensional for loops in php

    And continue;

    <?php
    $allowed_types = array(
        'image/gif',
        'image/jpeg',
        // add your mime types more
    );
    
    if (isset($_POST['newuser'])) {
        if ((!empty($_POST['year'])) AND ...
            // that gives an associative array
            // i think it's more handy in your case
            $files = assoc($_FILES['fisier']);
            // now you have a files like
            // tmp_name => d:\tmp\abs123, name => cats.jpg, size => 128 ..
            // tmp_name => d:\tmp\abs254, name => dogs.jpg, size => 211 ..
            // ... more code here
            // and here, remove this line
            if (!empty($_FILES['fisier']['name'])) {
            // put this. yes 6, cos you want 6 pics
            if (count($files) == 6) {
                // loop over files
                foreach ($files as $i => $file) {
                    // create targets, securely
                    $targets[$i] = 
                        'images/'. preg_replace('~[^\w\d\.]~i', '', 
                                        basename($file['name']));
                    // check some errors
                    if ($file['error'] == 0 && $file['size'] < 2048000
                            // check file type
                            && in_array($file['type'], $allowed_types)) {
                        if (!move_uploaded_file(
                                $file['tmp_name'], 'images/'. $file['name'])) {
                            $targets[$i] = 'images/default.jpg';
                            // collect messages
                            $messages[] = $file['name'] . ' not saved!';
                        }
                    }
                }
            }
            // and creating target 1 2 3 ...
            $targets_string = '';
            if (!empty($targets)) {
                // this provides: '$target', '$target2', '$target3' ...
                $targets_string = "'". join("', '", $targets) ."'";
            }
            // ... more code here
            // and sql part
            $sql = "INSERT INTO astonmartin VALUES('', '$year', '$make', ..."
                    // add target string
                    . "'$description', $targets_string";
            // ... more code here
            // msg part
            $msg = empty($messages) ? '' : join('', $messages);
            header("Location: add_user.php?msg=$msg");
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号