ขอสอบถามเรื่อง input file ค่ะ
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ขอสอบถามเรื่อง input file ค่ะ
ขอสอบถามเรื่อง input file ค่ะ
Copy
คือตอนนี้ได้นำโค้ดมาประยุกต์จากเว็บนี้นะค่ะ http://plugins.krajee.com/file-input/demo#ajax-uploads แล้วผลปรากฎว่า validate แจ้งว่า error ตลอดเลยค่ะ เป็นแบบนี้ค่ะ ไม่เป็นไปตามตัวอย่างในเว็บไซต์เลยค่ะ ถ้าอัพโหลดถูกไฟล์ มันจะ success ค่ะ แต่อันนี้ลองทำทั้งอัพลงดาต้าเบสก็แล้ว ก็ยังขึ้นเป็นสีแดงค่ะ ไม่รู้ต้องไปตั้งค่าตรงไหนส่วนไหนค่ะ แต่รูปพออัพลงดาต้าเบส อัพลงนะค่ะ แต่ก็ยังเป็นสีแดงๆอยู่ค่ะ
![]()
และส่วนอันนนี้เป็นโค้ดด้าน index.php ค่ะ
และส่วนอันนนี้เป็นโค้ดด้าน index.php ค่ะ
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>Fuck yeah</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/fileinput.min.css">
</head>
<body>
<section id="inv-body" class="center-block">
<div id="pedido" class="">
<form id="ticketec" class="" method="post" enctype="multipart/form-data">
<h4>Adjunta tu ticket <span class="glyphicon glyphicon-question-sign"></span></h4>
<input type="hidden" name="server" value="logged-user-server"></input>
<input type="hidden" name="user" value="logged-user"></input>
<div class="form-group">
<input id="images" name="images[]" type="file" multiple>
<div id="errorBlock43" class="help-block"></div>
</div>
<!--<input type="hidden" name="ticketenviado" value="si"></input>-->
<input id="userid" name="userid" type="hidden">
<input id="username" name="username" type="text">
<button type="submit" class="btn btn-danger center-block">Enviar</button>
</form>
<!--<form id="confirm" class="hidden" method="GET">
</form>-->
</div>
</section>
</div>
<script src="js/fileinput.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/fileinput.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
// Disable preview and customize your own error container and messages.
$("#images").fileinput({
//showPreview: false,
browseClass: "btn btn-danger btn-block",
showCaption: true,
showRemove: true,
showUpload: true,
previewFileType: "image",
browseLabel: "เลือกรูป",
browseIcon: '<i class="glyphicon glyphicon-picture"></i>',
allowedFileExtensions: ["jpg", "JPG", "jpeg", "JPEG", "png", "PNG", "pdf"],
elErrorContainer: "#errorBlock43",
msgInvalidFileExtension: 'El formato de "{name}" es incorrecto. Solo archivos "{extensions}" son admitidos.',
// you can configure `msgErrorClass` and `msgInvalidFileExtension` as well
//AJAX
dropZoneEnabled: false,
uploadAsync: false,
uploadUrl: "subir.php", // your upload server url
uploadExtraData: function() {
return {
userid: $("#userid").val(),
username: $("#username").val()
};
}
});
});
</script>
</body>
</html>
และส่วนอันนี้เป็น subir.php เพื่อทำการอัพโหลดไฟล์ค่ะ
<?
// upload.php
// 'images' refers to your file input name attribute
if (empty($_FILES['images'])) {
echo json_encode(['error'=>'No files found for upload.']);
// or you can throw an exception
return; // terminate
}
// get the files posted
$images = $_FILES['images'];
// get user id posted
$userid = empty($_POST['userid']) ? '' : $_POST['userid'];
// get user name posted
$username = empty($_POST['username']) ? '' : $_POST['username'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "/tickets/" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($images['tmp_name'][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {
// call the function to save all data to database
// code for the following function `save_data` is not
// mentioned in this example
save_data($userid, $username, $paths);
// store a successful response (default at least an empty array). You
// could return any additional response info you need to the plugin for
// advanced implementations.
$output = ['success'=>'Upload Success'];
// for example you can get the list of files uploaded this way
// $output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา
โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ