我无法使用PHP上传多个图像

时间:2013-02-01 01:10:17

标签: php mysql

  

可能重复:
  PHP - Upload multiple images

我想使用PHP上传多张图片而且我被卡住了。我有代码,但只上传了1张图片。

<?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 个答案:

答案 0 :(得分:0)

我认为您首先需要此功能assoc,此处:multidimensional for loops in php

继续;

<?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");
?>