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
<?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.";
}
}