上传多个文件

时间:2018-05-16 09:31:09

标签: php

所以主要的问题是我似乎无法将图像移动到指定的文件夹。我有这个记录名称和目录的数据库。它似乎运作良好。它只是无法移动到文件夹的图像。

这是HTML代码:

<div class="form-group" align="left">
<label for="aialbum">Album:</label>
<select class="form-control" id="aialbum" name="aialbum">
<?php
    $query = "SELECT * FROM cms_albums";
    $showalbums = mysqli_query($con, $query);
    while($getalbum = mysqli_fetch_array($showalbums))
    {
    $album = $getalbum['album_name'];
    echo "<option value='$album'>$album</option>";
    }
?>
</select>
<br><label for="aipicture">Image/s:</label>
<div class="form-group">
<input type='file' name='aipicture[]' id='aipicture[]' multiple>
</div>
</div>
<div align="right">
<input class="btn btn-default" type='submit' name='aisubmit' value='Submit'>
</div>

这是我的PHP代码:(更新)

if(isset($_POST['aisubmit']))
{
    $aialbum = $_POST['aialbum'];
    $aipicture = $_POST['aipicture'];
    $filecount = count($aipicture);

    for($i=0; $i<$filecount; $i++)
    {
        $temp_name = $_FILES[$aipicture[$i]]['tmp_name'];
        $org_name = $_FILES[$aipicture[$i]]['name'];
        $path = "../img";
        move_uploaded_file($temp_name, $path);
        $sql = "INSERT INTO cms_album_photos (album_name, picture) VALUES ('$aialbum', 'img/$aipicture[$i]')";
        mysqli_query($con, $sql);
    }
    echo '<script type="text/javascript">';
    echo 'window.location.href="home.php";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url=home.php" />';
    echo '</noscript>'; exit;
        }

2 个答案:

答案 0 :(得分:0)

这样做

$target_Path = "asd/".basename( $_FILES['fileToUpload']['name']);
move_uploaded_file( $_FILES['fileToUpload']['tmp_name'], $target_Path );

答案 1 :(得分:0)

改善您的代码

$temp_name = $_FILES[$aipicture[$i]]['tmp_name'];
$org_name = $_FILES[$aipicture[$i]]['name'];

到此

$temp_name = $_FILES[$aipicture]['tmp_name'][$i];
$org_name = $_FILES[$aipicture]['name'][$i];

希望这有帮助!