2
다음 PHP 파일 스크립트는 여러 파일을 보내 겠지만 한 파일 만 업로드합니다. 깨지는 부분을 이해할 수 없습니다. foreach
을 사용해 보았지만 아무 소용이 없습니다.여러 파일 업 로더를 사용하여 하나의 파일 만 업로드 중
내가 수행 한 파일은 var_dump
이며 올바른 파일 수가 표시됩니다.
HTML
<form id="fileupload" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadfile[]" multiple id="uploadfile" />
</form>
PHP
<?php
require_once 'config.php';
############ Edit settings ##############
$UploadDirectory = './storage/';
#########################################
if(!isset($_FILES["uploadfile"])) {
die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}
//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}
// Total number of files to be uploaded
$total_files = count($_FILES['uploadfile']['name']);
//check if there is at least 1 file
if($total_files < 1) {
die();
}
for($i=0; $i<$total_files; $i++) {
//Is file size is less than allowed size.
if ($_FILES["uploadfile"]["size"][$i] > 29000000) {
die("File size is too big!");
}
$allowedTypes = array('gif', 'png', 'jpeg', 'jpg', 'pdf', 'xls', 'xlsx', 'doc', 'docx', 'ppt', 'pptx', 'mp3', 'mp4', 'rar', 'zip', 'txt');
$FileNameFull = strtolower($_FILES['uploadfile']['name'][$i]);
$FileNameShort = pathinfo($FileNameFull, PATHINFO_FILENAME);
$FileExt = pathinfo($FileNameFull, PATHINFO_EXTENSION);
if(!in_array($FileExt, $allowedTypes)) {
die('Unsupported File format!');
}
$FileCode = rand(0, 9999999999); //Random number to be used to rename actual filename
$NewFileName = $FileCode.'.'.$FileExt; //new file name
if(move_uploaded_file($_FILES['uploadfile']['tmp_name'][$i], $UploadDirectory.$NewFileName)) {
// Save the file details to database
//$query = $dbh->prepare("INSERT INTO uploads(file_name, file_code, file_ext, timestamps) VALUES (:file_name, :file_code, :file_ext, :timestamps)");
/*$query->execute(array(
":file_name" => $FileNameShort,
":file_code" => $FileCode,
":file_ext" => $FileExt,
":timestamps" => round(microtime(true) * 1000)
));*/
die('Success! File Uploaded.');
}
else{
die('error uploading File!');
}
}
HTML을 게시하는 것이 좋습니다. – Noman
@Noman, 업데이트 됨! – Ayan
제게 그것은'! isset ($ _ SERVER [ 'HTTP_X_REQUESTED_WITH'])'에 멈추었지만 Ajax로 시도하지 않았습니다. 'HTTP_X_REQUESTED_WITH'는 항상 설정되지는 않습니다. 모든 JS 프레임 워크가 그러한 정보를 보내는 것은 아닙니다. – debute