PHP,上传2张图片,只上传1张

时间:2013-12-23 17:26:02

标签: php file-upload

我正在尝试上传2张图片。 AVATAR COVER IMG

当我选择头像和封面并点击确认时,只有头像上传 当我只选择封面时,封面上传

请注意,在mysql中,正确显示第二个图像的名称。但在上传文件夹中丢失了

我的代码:

<?php 
define("_VALID_PHP", true);
require("../lib/config.ini.php"); 

$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$ID = $_GET['id'];

$name = $_POST['name'];
$category = $_POST['category'];
$sort = $_POST['sort'];
$tagline = $_POST['tagline'];
$exp = $_POST['exp'];
$company = $_POST['company'];
$country = $_POST['country'];
$links = $_POST['links'];
$created = $_POST['created'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$add3 = $_POST['add3'];
$add4 = $_POST['add4'];
$add3 = $_POST['add5'];
$add4 = $_POST['add6'];
$notes = $_POST['notes'];

$uploaddir = '/data/web/creattium.com/sub/dir/admin/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$thegfi = $_FILES['userfile']['name'];  
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Upload failed";
}

$uploadfile2 = $uploaddir . basename($_FILES['userfile2']['name']);
$thegfi2 = $_FILES['userfile2']['name'];
if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Upload failed";
}

mysqli_query($mysqli, "UPDATE yees SET name='$name',category='$category',sort='$sort',tagline='$tagline',exp='$exp',company='$company',country='$country',links='$links',created='$created',add1='$add1',add2='$add2',add3='$add3',add4='$add4',add5='$add5',add6='$add6',notes='$notes' WHERE id='$ID'");

if(!empty($_FILES['userfile']['name'])){
    mysqli_query($mysqli, "UPDATE yees SET avatar='$thegfi' WHERE id='$ID'"); 
}else if(!empty($_FILES['userfile2']['name'])){
    mysqli_query($mysqli, "UPDATE yees SET cover='$thegfi2' WHERE id='$ID'");
}

header("location: editemploy.php?id=".$ID."");

ADDED html

<form method="post" action="edit_yee.php?id=<?php echo $id;?>" enctype="multipart/form-data">
         <?php 

             $a = mysql_query("SELECT * FROM yees WHERE id='$id'");
             while($r = mysql_fetch_object($a)){

         ?>
     <table cellpadding="0" cellspacing="0" class="forms">
        <thead>
          <tr>
            <th colspan="2" class="left">Editing New Employee </th>
          </tr>
        </thead>
        <tfoot>
          <tr>
            <td><input type="submit" class="button" value="Edit Employee"></td>
            <td><a href="index.php?do=yees" class="button-alt">Cancel</a></td>
          </tr>
        </tfoot>
        <tbody>
          <tr>
            <th>Name:</th>
            <td><input type="text" name="name" class="inputbox" size="55" value="<?php echo $r->name;?>"></td>
          </tr>
          <tr>
            <th>Avatar:</th>
            <td><input type="file" name="userfile" class="inputbox" style="width:337px;"></td>
          </tr>
          <tr>
            <th>Cover:</th>
            <td><input type="file" name="userfile2" class="inputbox" style="width:337px;"></td>
          </tr>
          <tr>
            <th>Category:</th>
            <td><input type="text" name="category"class="inputbox" size="55" value="<?php echo $r->category;?>"></td>
          </tr>
          <tr>
            <th>Sort:</th>
            <td><input type="text" name="sort"class="inputbox" size="55" value="<?php echo $r->sort;?>"></td>
          </tr>
          <tr>
            <th>Tagline:</th>
            <td><input type="text" name="tagline"class="inputbox" size="55" value="<?php echo $r->tagline;?>"></td>
          </tr>
          <tr>
            <th>Exp:</th>
            <td><input type="text" name="exp"class="inputbox" size="55" value="<?php echo $r->company;?>"></td>
          </tr>
          <tr>
            <th>Company:</th>
            <td><input type="text" name="company"class="inputbox" size="55" value="<?php echo $r->exp;?>"></td>
          </tr>
          <tr>
            <th>Country:</th>
            <td><input type="text" name="country"class="inputbox" size="55" value="<?php echo $r->country;?>"></td>
          </tr>
          <tr>
            <th>Links:</th>
            <td><input type="text" name="links"class="inputbox" size="55" value="<?php echo $r->links;?>"></td>
          </tr>
          <tr>
            <th>Created:</th>
            <td><input type="text" name="created"class="inputbox" size="55" value="<?php echo $r->created;?>"></td>
          </tr>
          <tr>
            <th>Add1:</th>
            <td><input type="text" name="add1"class="inputbox" size="55" value="<?php echo $r->add1;?>"></td>
          </tr>
          <tr>
            <th>Add2:</th>
            <td><input type="text" name="add2"class="inputbox" size="55" value="<?php echo $r->add2;?>"></td>
          </tr>
          <tr>
            <th>Add3:</th>
            <td><input type="text" name="add3"class="inputbox" size="55" value="<?php echo $r->add3;?>"></td>
          </tr>
          <tr>
            <th>Add4:</th>
            <td><input type="text" name="add4"class="inputbox" size="55" value="<?php echo $r->add4;?>"></td>
          </tr>
          <tr>
            <th>Add5:</th>
            <td><input type="text" name="add5"class="inputbox" size="55" value="<?php echo $r->add5;?>"></td>
          </tr>
          <tr>
            <th>Add6:</th>
            <td><input type="text" name="add6"class="inputbox" size="55" value="<?php echo $r->add6;?>"></td>
          </tr>
          <tr>
            <th>Notes:</th>
            <td><input type="text" name="notes"class="inputbox" size="55" value="<?php echo $r->notes;?>"></td>
          </tr>
        </tbody>
     </table><br />
        <?php }?>
   </form>

2 个答案:

答案 0 :(得分:1)

将输入重命名为:

<input type="file" name="userfile[]" class="inputbox" style="width:337px;"> 

然后,在php中使用:

$_FILES['userfile']['name'][0]
$_FILES['userfile']['name'][1]
...

此处有更多信息:http://www.php.net/manual/en/features.file-upload.multiple.php

同时检查:

  

自PHP 5.2.12起,max_file_uploads配置设置充当   限制一个请求中可以上载的文件数。您   需要确保您的表单不会尝试上传更多文件   在一个请求中超过此限制。

答案 1 :(得分:1)

<input type="file" name="userfile[]" class="inputbox" style="width:337px;">


而不是

<input type="file" name="userfile[]" class="inputbox" style="width:337px;" multiple>