도움이 필요합니다. 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);
}
}
}
?>
여기에서는 하나의 이미지 만 추가 할 수 있지만 여기에서는 여러 이미지를 업로드해야합니다. 이 문제를 해결하도록 도와주세요.
여러 속성 사용
다중 속성은 부울 속성입니다.
있는 경우 한 번에 여러 옵션을 선택할 수 있음을 지정합니다.
HTML 입력
<input style="padding:0;" type="file" class="filestyle form-control" data-size="lg" multiple name="bannerimage[]" id="bannerimage">
귀하의 PHP
// set all images into array
function reArrayFiles(&$file_post)
{
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i = 0; $i < $file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
if ($_FILES['bannerimage']['name'] != "") {
$file_ary = reArrayFiles($_FILES['bannerimage']);
foreach ($file_ary as $file) {
uploadImage($file, $imageName, $imagePath, function ($image) {
$newCustomerobj->image = $image['img'];
}
}
}
최신 정보
두 개의 개별 이미지 업로드
<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
$imageName = 'bannerimage';
$imageName2 = 'bannerimage1'; // for bannerimage1
$imagePath = "uploads/";
// uploading bannerimage
if ($_FILES['bannerimage']['name'] != "") {
uploadImage($_FILES, $imageName, $imagePath, function ($image) {
$newCustomerobj->image = $image['img'];
}
}
// uploading bannerimage1
if ($_FILES['bannerimage1']['name'] != "") {
uploadImage($_FILES, $imageName2, $imagePath, function ($image) {
$newCustomerobj->image = $image['img'];
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다