PHP多个上传上传较少的文件

时间:2016-05-30 14:12:20

标签: php forms loops multi-upload

我总是在这里找到对我的项目非常有用的问题和答案。我目前正在处理PHP多上传表单,并希望这个社区可以帮助我。到目前为止,代码工作,除了如果我上传多个文件,我上传的文件少一个。例如,如果我选择3个文件,我会上传2个文件,如果我选择8个文件则上传7个。以下是我的代码:

<?php
$id  = $_SESSION['PHOTO_ID'];
$hd  = $_SESSION['PROJECT_NAME'];
$cat = "projects";

//function to get file extension
function getExtension($str)
    {
        $i   = strrpos($str,".");
        if(!$i) {
            return "";
        }
        $l   = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;
    }

$allowedExts = array("jpeg", "jpg");
for ($i = 0; $i < count($_FILES['img']['name']); $i++) {
    //set variables for images
    $imgname = $_FILES["img"]["name"][$i];
    $img_tmp = $_FILES["img"]["tmp_name"][$i];
    $exts    = getExtension($_FILES["img"]["name"][$i]);

    //replace space character with a dash and add timestamp and random numbers

    $nm     = str_replace(" ","-",$hd);
    //$exts = getExtension($val['name']);
    $rand   = rand();
    $img    = $rand."-".$nm."-".time().".".$exts;
    $target = "../../img/".$img; 
    move_uploaded_file($img_tmp=$_FILES["img"]["tmp_name"][$i], $target);
}
//end of for loop
}
?>

html表格如下:

<form method="post" action="demo.php"  id="imageform" role="form" enctype="multipart/form-data">
    <input type="file" multiple name="img[]"  id="images">
    <input type="submit" name="submit"  id="image_upload" value="Update">
</form>

我创建的会话,但目前并没有用。

$_SESSION['PHOTO_ID']     = $_GET['id'];
$_SESSION['PROJECT_NAME'] = $_GET['project'];

0 个答案:

没有答案