$ _FILES ['文件'] ['名称']不显示任何内容

时间:2015-11-09 08:42:48

标签: php

在我的项目中,我使用多个选项上传图像,但文件名显示为空白。这是我的代码。

<?php include_once("../../../init.php");
ob_start();
if(isset($_SESSION['user']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $inc= -1;
    if($_POST['cid'] == '') {
        $inc++; $_SESSION['error'][$inc] = "COM ID IS REQUIRED";
        header("location:../../views/tileesDesign/list.php");
    }
    if($_POST['cat'] == '') {
        $inc++; $_SESSION['error'][$inc] = "CATEGORY IS REQUIRED";
        header("location:../../views/tileesDesign/list.php");
    }
    if($_POST['productSize'] == '') {
        $inc++; $_SESSION['error'][$inc] = "SIZE IS REQUIRED";
        header("location:../../views/tileesDesign/list.php");
    }
    if($_POST['sequence'] == '') {
        $inc++; $_SESSION['error'][$inc] = "SEQUENCE IS REQUIRED";
        header("location:../../views/tileesDesign/list.php");
    }
    if($_POST['albumName'] == '') {
        $inc++; $_SESSION['error'][$inc] = "ALBUM NAME IS REQUIRED";
        header("location:../../views/tileesDesign/list.php");
    }
    else if($_POST['albumName'] != '' && preg_match("/[^0-9a-zA-Z ]/", $_POST['albumName'])) {
        $inc++; $_SESSION['error'][$inc] = "INVALID ALBUM NAME";
        $_SESSION['values']['albumName'] = $_POST['albumName'];
        header("location:../../views/tileesDesign/list.php");
    }
    else {
        require_once("../../includes/config.php");
        $companyDetail = mysql_query("SELECT * FROM company_details WHERE com_id='".$_POST['cid']."'");
        $des_img="../../../public/img/design/";
        if(mysql_num_rows($companyDetail) == 1) {
            if(!is_dir($des_img.mysql_result($companyDetail, 0, "com_email"))) mkdir($des_img . mysql_result($companyDetail, 0, "com_email"),0777,true);
            if(!is_dir($des_img.mysql_result($companyDetail, 0, "com_email")."/".$_POST['albumName'])) {
                $valid_formats = array("jpg", "png", "gif", "jpeg", "bmp", "JPG", "PNG", "GIF", "JPEG", "BMP");
                $max_file_size = 1048576;
                $count = 0; $value = ''; $flag = 0;
                function compress_image($source_url, $destination_url, $quality) {
                    $info = getimagesize($source_url);
                    if ($info['mime'] == 'image/jpeg')
                        $image = imagecreatefromjpeg($source_url);
                    elseif ($info['mime'] == 'image/gif')
                        $image = imagecreatefromgif($source_url);
                    elseif ($info['mime'] == 'image/png')
                        $image = imagecreatefrompng($source_url);
                    elseif ($info['mime'] == 'image/png')
                        $image = imagecreatefromwbmp($source_url);

                    // load the image you want to you want to be watermarked
                      $watermark = imagecreatefrompng('../../../public/img/watermark.png');

                      // get the width and height of the watermark image
                      $water_width = imagesx($watermark);
                      $water_height = imagesy($watermark);

                      // get the width and height of the main image image
                      $main_width = imagesx($image);
                      $main_height = imagesy($image);

                      $im_middle_w = $main_width/2;
                      $im_middle_h = $main_height/2;

                      // Set the dimension of the area you want to place your watermark we use 0
                      // from x-axis and 0 from y-axis 
                      $dime_x = $im_middle_w - $water_width/2;
                      $dime_y = $im_middle_h - $water_height/2;

                      // copy both the images
                      imagecopy($image, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);
                    imagejpeg($image, $destination_url, $quality);
                }
                foreach ($_FILES['product']['name'] as $f => $name) {
                    $flag++;
                    if ($_FILES['product']['error'][$f] == 0) {
                        if ($_FILES['product']['size'][$f] > $max_file_size) {
                            $inc++;$_SESSION['error'][$inc] = "$name is too large!.";continue;
                        }
                        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ) {
                            $inc++;$_SESSION['error'][$inc] = "$name is not a valid format";continue;
                        }
                        else {
                            if(!is_dir($des_img.mysql_result($companyDetail, 0, "com_email")."/".$_POST['albumName'])) mkdir($des_img.mysql_result($companyDetail, 0, "com_email")."/".$_POST['albumName'],0777,true);
                            compress_image($_FILES["product"]["tmp_name"][$f],$des_img.mysql_result($companyDetail, 0, "com_email")."/".$_POST['albumName']."/".$name,50);
                            $count++;
                            $value .= "(NULL,".mysql_result($companyDetail, 0, "com_id").",'".$_POST['albumName']."','$name','".$_POST['productSize']."','".$_POST['cat']."','".$_POST['sequence']."'),";
                        }
                    }
                }
                if($count > 0) {
                    mysql_query("INSERT INTO design VALUES".substr($value,0,-1));
                    $_SESSION['message'] = $count . " ALBUM SUCCESSFULLY UPLOADED";
                }
                if($flag < 2 && $count == 0) {
                    $inc++; $_SESSION['error'][$inc] = "ALBUM IMAGE IS REQUIRED";
                    $_SESSION['values']['albumName'] = $_POST['albumName'];
                }
                header("location:../../views/tileesDesign/list.php");
            } else {
                $inc++;$_SESSION['error'][$inc] = "Abum '".$_POST['albumName']."' Already Existed.";
                $_SESSION['values']['albumName'] = $_POST['albumName'];
                header("location:../../views/tileesDesign/list.php");
            }       
        } else header("location:../user/out.php");
    }
} else header("location:../user/out.php");
?>

和html代码是。

<form action="../../controllers/tileesDesign/add.php" method="post" class="form-horizontal addSizeForm" id="addSize">
                            <div class="form-group">
                                <label for="cid" class="col-sm-2 control-label">Com ID:</label>
                                <div class="col-sm-1">
                                    <input type="text" name="cid" id="cid" class="form-control" placeholder="Com ID" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['cid']; ?>" />
                                </div>
                                <label for="cat" class="col-sm-1 control-label">Category</label>
                                <div class="col-sm-4">
                                    <select name="cat" class="form-control">
                                        <option selected value="">Select Category</option>
                                        <?php
                                            $categoryResult = mysql_query("SELECT * FROM `category` where flag = 1 order by `sequence`");
                                            while($categoryRow = mysql_fetch_assoc($categoryResult))
                                                echo '<option value="'.$categoryRow['cid'].'">'.$categoryRow['cat_name'].'</option>';
                                        ?>
                                    </select>
                                </div>
                                <label for="productSize" class="col-sm-1 control-label">Size</label>
                                <div class="col-sm-2">
                                    <select name="productSize" id="productSize" class="form-control">
                                        <option selected value="">Select Size(CM)</option>
                                        <?php
                                            $sizeResult = mysql_query("SELECT * FROM sizes ORDER BY sequence");
                                            while($sizeRow = mysql_fetch_assoc($sizeResult))
                                                echo '<option value="'.$sizeRow['id'].'">'.$sizeRow['inch'].'</option>';
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="albumName" class="col-sm-2 control-label">Add Album:</label>
                                <div class="col-sm-3">
                                    <input type="text" name="albumName" id="albumName" class="form-control" placeholder="Album Name" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['albumName']; ?>" />
                                </div>
                                <label for="sequence" class="col-sm-1 control-label">Sequence</label>
                                <div class="col-sm-1">
                                    <input type="text" name="sequence" id="sequence" class="form-control" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['sequence']; ?>" />
                                </div>
                                <div class="col-sm-3">
                                    <input type="file" name="product[]" id="product" multiple="multiple" style="display: inline-block;line-height:32px;" />
                                </div>
                                <div class="col-sm-2">
                                    <div class="col-sm-2"><input type="submit" class="btn btn-info" value="Add" name="submit" /></div>
                                </div>
                            </div>
                        </form>

但是当我提交表单时,它会替换&#34; ALBUM IMAGE是必需的&#34; ,当我显示$_FILES['product']['name']时,它显示空白或null但是它是如何& #39; s发生了???请帮助我。

1 个答案:

答案 0 :(得分:2)

要上传文件,我们在表单标记

中使用enctype="multipart/form-data">

所以而不是

<form action="../../controllers/tileesDesign/add.php" method="post" class="form-horizontal addSizeForm" id="addSize">

使用

<form action="../../controllers/tileesDesign/add.php" method="post" class="form-horizontal addSizeForm" id="addSize" enctype="multipart/form-data">