 |
แสดงผลว่า Resource id #7 จากการอัพโหลดรูป คืออะไรคะ ใช่เออเร่อรึเปล่าคะ |
|
 |
|
|
 |
 |
|
Code (PHP)
define ('MAX_FILE_SIZE', 220 * 145);
if (array_key_exists('upload', $_POST)) {
define('UPLOAD_DIR', '../upload/product_cate/');
$file = str_replace(' ', '_', $_FILES['image']['name']);
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg','image/png');
if (in_array($_FILES['image']['type'], $permitted)
&& $_FILES['image']['size'] > 0
&& $_FILES['image']['size'] <= MAX_FILE_SIZE) {
switch($_FILES['image']['error']) {
case 0:
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR . $file)) {
// move the file to the upload folder and rename it
$success =move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .$file);
} else {
$result = 'A file of the same name already exists.';
}
if ($success) {
$result = "$file uploaded successfully.";
} else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
case 6:
case 7:
case 8:
$result = "Error uploading $file. Please try again.";
break;
case 4:
$result = "You didn't select a file to be uploaded.";
}
} else {
$result = "$file is either too big or not an image.";
}
}
code upload ค่ะ ลอง echo $result ขึ้นว่า Resource id #7 คืออะไรคะ ลองลบโค้ดอัพโหลดดูก็ยังขึ้น Resource id #7
ขอบคุณค่ะ
Tag : PHP, HTML/CSS
|
|
 |
 |
 |
 |
Date :
2013-05-11 20:43:43 |
By :
pharkram |
View :
1301 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพราะ $result เป็นตัวแปรที่ใช้ก่อนหน้านี้ครับ และคงมาจากการเรียก mysql_query()
และที่ $result ไม่มีการเปลี่ยนค่าเป็นอย่างอื่น (ใน switch) มีได้สองเหตุผล
คือ
1 ไม่เข้าเงื่อนไขแรก if (array_key_exists('upload', $_POST))
2 ไม่เข้าเงื่อนไขใน switch switch($_FILES['image']['error']) {
ซึ่งควรเปลี่ยน case 3 - 8 เป็น default: จะได้มั่นใจว่า ไม่ว่า error จะเป็นโค้ดใด นอกจาก 0 และ 4 ก็จะมีการกำหนดค่า $result
Code (PHP)
<?php
define ('MAX_FILE_SIZE', 220 * 145);
if (array_key_exists('upload', $_POST))
{
define('UPLOAD_DIR', '../upload/product_cate/');
$file = str_replace(' ', '_', $_FILES['image']['name']);
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg','image/png');
if (in_array($_FILES['image']['type'], $permitted)
&& $_FILES['image']['size'] > 0
&& $_FILES['image']['size'] <= MAX_FILE_SIZE)
{
switch($_FILES['image']['error']) {
case 0:
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR . $file)) {
// move the file to the upload folder and rename it
$success =move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .$file);
} else {
$result = 'A file of the same name already exists.';
}
if ($success) {
$result = "$file uploaded successfully.";
} else {
$result = "Error uploading $file. Please try again.";
}
break;
case 4:
$result = "You didn't select a file to be uploaded.";
break;
default:
$result = "Error uploading $file. Please try again.";
break;
}
} else {
$result = "$file is either too big or not an image.";
}
}
|
 |
 |
 |
 |
Date :
2013-05-11 21:03:55 |
By :
cookiephp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2013-05-11 21:07:38 |
By :
pharkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|