简体   繁体   中英

PHP, uploading 2 images, only uploads 1

I'm trying to upload 2 images at time. AVATAR and COVER IMG

When I select both avatar and cover and hit confirm, only avatar uploads When I select only cover, cover uploads

Note that in mysql the name of the second image is displayed correctly. But missing in upload folder

My code:

<?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>

Rename your inputs as:

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

Then, in php use:

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

More info here: http://www.php.net/manual/en/features.file-upload.multiple.php

Also check this:

Since PHP 5.2.12, the max_file_uploads configuration setting acts as a limit on the number of files that can be uploaded in one request. You will need to ensure that your form does not try to upload more files in one request than this limit.

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


代替

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM