ถามเกี่ยวกับการส่งไฟล์ภาพใน Ajax ค่ะ ติดมาหลายวันแล้วค่ะ
อยากรู้ว่าทำไมเวลาเราส่งไฟล์ภาพไปโดยการ submit การใช้คำสั่ง print_r($_FILES); มันแสดงผลถูกต้องค่ะ copy ได้ แต่ถ้าเป็นการส่งแบบนี้มันจะให้ค่าเป็นแบบนี้ค่ะ Array() แก้มาทุกวิธีแล้วค่ะ set 777 แล้วทุกโฟลเดอร์ก็ไม่ได้ค่ะ รบกวนหน่อยค่ะ
อันนี้คือไฟล์แรก admin.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="modules/Schools/js/addGroupSubject.js"></script>
<body onLoad="send_group_subject_data();">
<table width="99%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="100%">
<form action="#school" method="get" name="fSchools" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td><img src="../../images/linkicon.gif" width="9" height="9" /><b>
<?=$mod?>
</b></td>
<td> </td>
<td><input name="hddID" type="hidden" id="hddID">
<input name="hddFile" type="hidden" id="hddFile"></td>
</tr>
<tr>
<td width="426"> </td>
<td width="234">รหัสคอร์สโหราศาสตร์</td>
<td width="545"><input name="code" type="text" id="code" size="3" maxlength="8" /> </td>
</tr>
<tr>
<td> </td>
<td>ชื่อคอร์สโหราศาสตร์</td>
<td><input name="name" type="text" id="name" maxlength="50" /></td>
</tr>
<tr>
<td> </td>
<td valign="top">รายละเอียดคอร์สโหราศาสตร์</td>
<td><textarea name="des" cols="30" rows="4" id="des"></textarea></td>
</tr>
<tr>
<td> </td>
<td>สถานะคอร์สโหราศาสตร์</td>
<td><input name="gs_status" type="radio" value="0" checked>
ฟรี
<input name="gs_status" type="radio" value="1">
จ่ายเงิน </td>
</tr>
<tr>
<td> </td>
<td>รูปโลโก้ประจำคอร์ส</td>
<td><div id="showpic"></div>
<input name="UserFile" type="file" id="UserFile" />
(50*35 pixels)</td>
</tr>
<tr>
<td><a name="school"></a></td>
<td> </td>
<td><input name="btnAddGroupSubject" type="button" id="btnAddGroupSubject" onClick="chknull();" value="เพิ่ม"/>
<input name="btnAddGroupSubject2" type="button" id="btnEditGroupSubject" onClick="editGroupSubject(fSchools.code.value,fSchools.name.value,fSchools.des.value,fSchools.UserFile.value,fSchools.hddID.value,fSchools.hddFile.value);" value="แก้ไข"/></td>
</tr>
</table>
</form>
<table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr>
<td><div id="group_subject"></div></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
อันนี้คือไฟล์ addGroupSubject.js
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
var req = Inint_AJAX();
function chknull(){
if(document.fSchools.code.value==""){
alert("โปรดระบุรหัสกลุ่ม");
document.fSchools.code.focus();
}else if(document.fSchools.name.value==""){
alert("โปรดระบุชื่อกลุ่มวิชา");
document.fSchools.name.focus();
}else if(document.fSchools.des.value==""){
alert("โปรดระบุรายละเอียด");
document.fSchools.des.focus();
}else if(document.fSchools.UserFile.value.length != 0){
if((document.fSchools.UserFile.value.indexOf(".jpg")==-1) &&(document.fSchools.UserFile.value.indexOf(".gif")==-1)&&(document.fSchools.UserFile.value.indexOf(".bmp")==-1)&&(document.fSchools.UserFile.value.indexOf(".jpeg")==-1)){
alert("กรุณาเลือกเฉพาะไฟล์ที่อนุญาติเท่านั้น .jpg ,.gif ,.bmp !");
document.fSchools.UserFile.focus();
}else{
addGroupSubject(fSchools.code.value,fSchools.name.value,fSchools.des.value,fSchools.gs_status.value,fSchools.UserFile.value);
}
}else{
addGroupSubject(fSchools.code.value,fSchools.name.value,fSchools.des.value,fSchools.gs_status.value,fSchools.UserFile.value);
}
}//function chknull(){
function addGroupSubject(code,name,des,gsstatus,UserFile){
req.open("GET","modules/Schools/js/addGroupSubject.php?code="+code+"&name="+name+"&des="+des+"&gsstatus="+gsstatus+"&UserFile="+UserFile);
document.fSchools.btnAddGroupSubject.disabled = true;
document.getElementById("btnAddGroupSubject").value="กำลังบันทึก";
req.onreadystatechange = function() {
if (req.readyState==4) {
if (req.status==200) {
var ret=req.responseText;
if (ret == "error") {
alert("Error");
} else if (ret == "dup"){
send_group_subject_data();
document.fSchools.btnAddGroupSubject.disabled = false;
alert("รหัสกลุ่มซ้ำ");
} else if (ret == "complete"){
send_group_subject_data();
clear_form();
document.fSchools.btnAddGroupSubject.disabled = false;
document.getElementById("btnAddGroupSubject").value="เพิ่ม";
//alert("เพิ่มข้อมูลแล้ว");
} else {
alert(ret);
}
}
}
};
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
req.send(null);
}//function addGroupSubject(code,name,des,UserFile){
function clear_form() {
//document.fSchools.code.value = "";
//document.fSchools.name.value = "";
//document.fSchools.des.value = "";
//document.fSchools.UserFile.value = "";
document.getElementById('fSchools').reset();
}
function send_group_subject_data() {
req.open('GET', 'modules/Schools/js/group_subject_data.php', true);
req.onreadystatechange = function() {
if (req.readyState==4) {
if (req.status==200) {
var data=req.responseText;
//alert(data);
document.getElementById("group_subject").innerHTML=data;
}
} else {
document.getElementById("group_subject").innerHTML='<img src="images/loading.gif" alt="" > <span class="text_ms_san_black">กรุณารอสักครู่</span>';
}
};
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
req.send(null);
document.fSchools.btnEditGroupSubject.style.display = 'none';//ไม่ใชว์ปุ่มแก้ไข
};
function delGroupSubject(id,pic,name){
var del = confirm("คุณต้องการที่จะลบ "+name+" ?");
if(del == true){
req.open('GET', 'modules/Schools/js/group_subject_data.php?action=del&id='+id+"&pic="+pic, true);
req.onreadystatechange = function() {
if (req.readyState==4) {
if (req.status==200) {
var data=req.responseText;
//alert(data);
document.getElementById("group_subject").innerHTML=data;
}
} else {
document.getElementById("group_subject").innerHTML='<img src="images/loading.gif" alt="" > <span class="text_ms_san_black">กรุณารอสักครู่</span>';
}
};
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
req.send(null);
}//if(del == true){
}
function editShowGroupSubject(id,code,name,des,gsstatus,pic){
document.fSchools.btnAddGroupSubject.style.display = 'none'; //ไม่ใชว์ปุ่มเพิ่ม
document.fSchools.btnEditGroupSubject.style.display = '';//ใชว์ปุ่มแก้ไข
document.fSchools.hddID.value = id;
document.fSchools.hddFile.value = pic;
document.fSchools.code.value = code;
document.fSchools.name.value = name;
document.fSchools.des.value = des;
if(gsstatus=="1"){
document.fSchools.gs_status[0].checked = true;
}else{
document.fSchools.gs_status[1].checked = true;
}
if(pic){
document.getElementById("showpic").innerHTML="<img src ='modules/Schools/js/fileupload/" + pic + "'>";
}
}
function editGroupSubject(code,name,des,UserFile,id,oldfile){
if(fSchools.gs_status[0].checked == true){
var gsstatus ="1";
}else{
var gsstatus ="0";
}
req.open('GET', 'modules/Schools/js/group_subject_data.php?action=edit&id='+id+"&code="+code+"&name="+name+"&des="+des+"&UserFile="+UserFile+"&oldfile="+oldfile+"&gsstatus="+gsstatus,true);
req.onreadystatechange = function() {
if (req.readyState==4) {
if (req.status==200) {
var data=req.responseText;
//alert(data);
document.getElementById("group_subject").innerHTML=data;
}
} else {
document.getElementById("group_subject").innerHTML='<img src="images/loading.gif" alt="" > <span class="text_ms_san_black">กรุณารอสักครู่</span>';
}
};
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
req.send(null);
clear_form();
document.getElementById("showpic").innerHTML="";
document.fSchools.btnAddGroupSubject.style.display = '';//ใชว์ปุ่มเพิ่ม
document.fSchools.btnEditGroupSubject.style.display = 'none';//ไม่ใชว์ปุ่มแก้ไข
}
อันนี้ไฟล์ addGroupSubject.php
<?
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("Cache-control: private");
header("content-type: text/html; charset=tis-620");
include("../../../class/class.db.ajax.php");
include("../../../class/class.general.php");
include("../../../class/initail.php");
include("../../../class/class.upload.php");
$Path = "fileupload/";
$db = new DB("$hostname","$user","$pass","$dbase");
$num_row = $db->num_rows('*','group_subject',"gs_is ='$code'",null,null,null);
if($num_row >= 1)
{
$dubplicat_email = "dup";
echo"$dubplicat_email";
}
else
{
$detail = ereg_replace('<[^>]*>','',$description);
//-----------------------------------ค่าที่ได้จาการ Random --------------------------------------------------------
$length = 18;
$template = "1234567890";
global $template;
settype($length, "integer");
settype($rndstring, "string");
settype($a, "integer");
settype($b, "integer");
for ($a = 0; $a <= $length; $a++) {
$b = rand(0, strlen($template) - 1);
$number_randxx .= $template[$b];
}
//-----------------------------------ค่าที่ได้จาการ Random --------------------------------------------------------
if($UserFile){
-----------------------------อันนี้เป็นแบบนึง-------------------------------------------------------------------------
ในส่วนนี้เวลาขึ้นโฮสมันก็ copy ไม่ได้ค่ะ มันบอก copy(C:\AppServ\www ravel\admin\image\productb5.jpg (สมมติpath ค่ะ)
$UserFileOriginal = addslashes($UserFile); //C:\AppServ\www ravel\admin\image\productb5.jpg
$arryFileDestination = explode("\",$UserFile);
$arryFileDestinationed = count($arryFileDestination)-1;
$newFile = $number_randxx.$arryFileDestination[$arryFileDestinationed];
copy($UserFileOriginal,"fileupload/$newFile");
----------------------------------------------------------------------------------------------------------------------
----------------------------------------ลองใช้อีกแบบก็มีปัญหา Array()------------------------------
$UserFile =$_FILES['UserFile']['name'];
$file_type =$UserFile;
$file_type_length = strlen($file_type) - 3;
$file_type = substr($file_type, $file_type_length);
$file_temp = $_FILES['UserFile']['tmp_name'];
echo $_FILES['UserFile']['tmp_name'];
print_r($_FILES);
$newFile =$number_randxx.$_FILES['UserFile']['name'];
copy($_FILES[UserFile][tmp_name],"modules/Schools/js/fileupload/$number_randxx".$_FILES[UserFile][name]);
---------------------------------------------------------------------------------------------------------------------------------------------------
}
$result=$db->add('group_subject',array("gs_id"=>"$code","gs_name"=>"$name","gs_des"=>"$des","gs_status"=>"$gsstatus","img"=>"$newFile"));
if ($result) {
echo "complete";
} else {
echo "error";
}
}
$db->disconnect();
?>
ช่วยหน่อยน่ะค่ะ
Ajax ไม่สามารถส่งไฟล์ได้โดยตรง ถ้าอยากอัปโหลดให้ดูที่ GForm