【发布时间】:2017-05-17 12:04:57
【问题描述】:
我需要一些帮助。我需要使用 PHP 将多个文件上传到文件夹中。在这里我只能上传单个文件。我在下面解释我的代码。
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage" id="bannerimage">
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" name="bannerimage1" id="bannerimage1">
这是我的 PHP 代码。
<?php
$imageName='bannerimage';
$imagePath="uploads/";
if ($_FILES['bannerimage']['name'] != ""){
uploadImage($_FILES,$imageName,$imagePath,function($image){
$newCustomerobj->image =$image['img'];
}
}
public function uploadImage($files, $imageFieldName, $imageDirPath, $callback) {
$result = array();
// print_r( $_FILES);exit;
$imageName = generateRandomNumber() . '_' . $_FILES[$imageFieldName]['name'];
// echo($_FILES[$imageFieldName]['tmp_name']);exit;
$target_dir = $imageDirPath;
$target_file = $target_dir . basename($imageName);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if (file_exists($target_file)) {
$result['msg'] = "Sorry, file already exists.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if ($_FILES[$imageFieldName]["size"] > 500000) {
// echo 'fileSize';exit;
$result['msg'] = "Sorry, file size is large.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if (($imageFileType != "jpg" && $imageFileType != "JPG") && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$result['msg'] = "Sorry, only .jpg,.jpeg,.gif and .png files are allowed.";
$result['num'] = 0;
$callback($result);
$uploadOk = 0;
}
if ($uploadOk == 0) {
$result['msg'] = "Sorry, Your file could not uploaded.";
$result['num'] = 0;
$callback($result);
} else {
if (move_uploaded_file($_FILES[$imageFieldName]['tmp_name'], $target_file)) {
$result['msg'] = "Image has uploaded successfully.";
$result['num'] = 1;
$result['img'] = $imageName;
$callback($result);
} else {
$result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
$result['num'] = 0;
$callback($result);
}
}
}
?>
这里我只能添加一张图片,但这里我需要上传多张图片。请帮我解决这个问题。
【问题讨论】:
-
请参考php.net/manual/en/features.file-upload.multiple.php,然后在php端尝试打印$_FILES。你会得到答案
-
在你的html表单中,把你的名字改成
files[]
标签: php