class upload รูปภาพ |
|
http://www.select2web.com/php/upload-image-with-php.html
การอัปโหลดรูปภาพเก็บในฐานข้อมูล มีวิธีทำ 2 วิธี คือ
ทั้งสองวิธีนี้ต่างมีข้อดีและข้อเสีย วิธีที่ 1 ข้อดีคือ ฐานข้อมูลไม่บวม เพราะเก็บเฉพาะชื่อรูปภาพเท่านั้น ส่วนข้อเสียคือ โฟลเดอร์ที่ใช้เก็บรูปภาพ ต้องกำหนด permission เป็น 0777 เขียนและอ่านได้ ซึ่งในบางสภาพแวดล้อม เป็นเรื่องที่ลำบาก โดยเฉพาะกับโฮสต์ที่เป็นวินโดว ต้องติดต่อ admin ให้ช่วย ช่างวุ่นวาย วิธีที่ 2 ข้อดีคือ ไม่ต้องสนใจ permisstion ของโฟลเดอร์ที่เก็บรูปภาพ ส่วนข้อเสียคือ ฐานข้อมูลจะบวมใหญ่ โดยใช่เหตุ ถ้าระบบเล็กๆก็ไม่เป็นไรหรอก แต่ถ้าระบบใหญ่ๆ มีปัญหาแน่
จะกล่าวถึงวิธีที่ 1 ก่อน ซึ่ง class.upload.php นี้ ความสามารถสูงส่ง ทั้งใช้ง่าย ความสามารถโดยคร่าวๆ นะ
ดาวน์โหลด class.upload.php ที่ http://www.verot.net/php_class_upload_download_zip.htm
อ่าน help เพิ่มเติมที่ http://www.verot.net/php_class_upload_docs.htm โครงสร้างฐานข้อมูลในตัวอย่าง <?php // Include คลาส class.upload.php เข้ามา เพื่อจัดการรูปภาพ require_once('class/class.upload.php') ; // ส่วนกำหนดการเชื่อมต่อฐานข้อมูล $hostname_connection = "localhost"; $database_connection = "db_test"; $username_connection = "root"; $password_connection = ""; $connection = mysql_pconnect($hostname_connection, $username_connection, $password_connection) or trigger_error(mysql_error(),E_USER_ERROR); mysql_query( "SET NAMES UTF8" ) ; // ถ้าหากหน้านี้ถูกเรียก เพราะการ submit form // ประโยคนี้จะเป็นจริงกรณีเดียวก็ด้วยการ submit form if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { // เริ่มต้นใช้งาน class.upload.php ด้วยการสร้าง instant จากคลาส $upload_image = new upload($_FILES['image_name']) ; // $_FILES['image_name'] ชื่อของช่องที่ให้เลือกไฟล์เพื่ออัปโหลด // ถ้าหากมีภาพถูกอัปโหลดมาจริง if ( $upload_image->uploaded ) { // ย่อขนาดภาพให้เล็กลงหน่อย โดยยึดขนาดภาพตามความกว้าง ความสูงให้คำณวนอัตโนมัติ // ถ้าหากไม่ต้องการย่อขนาดภาพ ก็ลบ 3 บรรทัดด้านล่างทิ้งไปได้เลย $upload_image->image_resize = true ; // อนุญาติให้ย่อภาพได้ $upload_image->image_x = 400 ; // กำหนดความกว้างภาพเท่ากับ 400 pixel $upload_image->image_ratio_y = true; // ให้คำณวนความสูงอัตโนมัติ $upload_image->process( "upload_images" ); // เก็บภาพไว้ในโฟลเดอร์ที่ต้องการ *** โฟลเดอร์ต้องมี permission 0777 // ถ้าหากว่าการจัดเก็บรูปภาพไม่มีปัญหา เก็บชื่อภาพไว้ในตัวแปร เพื่อเอาไปเก็บในฐานข้อมูลต่อไป if ( $upload_image->processed ) { $image_name = $upload_image->file_dst_name ; // ชื่อไฟล์หลังกระบวนการเก็บ จะอยู่ที่ file_dst_name $upload_image->clean(); // คืนค่าหน่วยความจำ // เก็บชื่อภาพลงฐานข้อมูล $insertSQL = sprintf("INSERT INTO tbl_image (image_name) VALUES ( '%s' )", $image_name ); echo $insertSQL ; mysql_select_db($database_connection, $connection); $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error()); }// END if ( $upload_image->processed ) }//END if ( $upload_image->uploaded ) } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload Image To Database</title> </head> <body> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <p>Image_name <input name="image_name" type="file" id="image_name" size="40" /> </p> <p> <input type="submit" value="Upload" /> <input type="hidden" name="MM_insert" value="form1" /> </p> </form> </body> </html> |
class php upload รูป
|
[ ความเห็นที่ 1]
RE : class upload รูปภาพ โพสต์เมื่อ: วันพฤหัสบดี 22 ธันวาคม 2554 22:12 น. ปัญหา upload ไฟล์ ไม่ได้ซักที เนื่องจากไฟล์ขนาดใหญ่เกินไป
แก้ไขโดย เอา ตรงนี้ออก ครับ <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> ไม่ต้องใส่ก็ ได้ สรุปแล้ว MAX_FILE_SIZE เนี่ยไม่ใส่ก็ได้ครับ เพราะมันจะไปตันที php.ini แทน และ ถึงจะใส่ MAX_FILE_SIZE ไปมากกว่าที่กำหนดใน php.ini ก็ยังอัพโหลดได้ไม่เกิน php.ini อยู่ดี |
ปัญหา upload รูปไม่ได้ |
[ ความเห็นที่ 2]
RE : class upload รูปภาพ โพสต์เมื่อ: วันพฤหัสบดี 9 เมษายน 2558 20:41 น.
ถ้าต้องการระบุ นามสกุลไฟล์ ไหน ที่จะ upload ได้บ้าง
ลองใช้ $upload_image_thumb->allowed = array('application/pdf','application/msword', 'image/*', 'application/excel'); หรือ $upload_image_thumb->allowed_types = "jpg|gif|png|txt|xls|doc|pdf"; ถ้าต้องการดูว่า มีไฟล์ อะไร ที่ upload ได้บ้าง เข้าไปที่ class.upload.php ค้นหา function init() จะเป็น default ของ class นี้ |
[ ความเห็นที่ 3]
RE : class upload รูปภาพ โพสต์เมื่อ: วันพฤหัสบดี 9 เมษายน 2558 20:47 น.
ดูตามนี้
$this->allowed = array( 'application/arj', 'application/excel', 'application/gnutar', 'application/mspowerpoint', 'application/msword', 'application/octet-stream', 'application/onenote', 'application/pdf', 'application/plain', 'application/postscript', 'application/powerpoint', 'application/rar', 'application/rtf', 'application/vnd.ms-excel', 'application/vnd.ms-excel.addin.macroEnabled.12', 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'application/vnd.ms-excel.sheet.macroEnabled.12', 'application/vnd.ms-excel.template.macroEnabled.12', 'application/vnd.ms-office', 'application/vnd.ms-officetheme', 'application/vnd.ms-powerpoint', 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'application/vnd.ms-powerpoint.slide.macroEnabled.12', 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'application/vnd.ms-word', 'application/vnd.ms-word.document.macroEnabled.12', 'application/vnd.ms-word.template.macroEnabled.12', 'application/vnd.oasis.opendocument.chart', 'application/vnd.oasis.opendocument.database', 'application/vnd.oasis.opendocument.formula', 'application/vnd.oasis.opendocument.graphics', 'application/vnd.oasis.opendocument.graphics-template', 'application/vnd.oasis.opendocument.image', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.oasis.opendocument.presentation-template', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.spreadsheet-template', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.text-master', 'application/vnd.oasis.opendocument.text-template', 'application/vnd.oasis.opendocument.text-web', 'application/vnd.openofficeorg.extension', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.presentationml.slide', 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'application/vnd.openxmlformats-officedocument.presentationml.template', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'application/vocaltec-media-file', 'application/wordperfect', 'application/x-bittorrent', 'application/x-bzip', 'application/x-bzip2', 'application/x-compressed', 'application/x-excel', 'application/x-gzip', 'application/x-latex', 'application/x-midi', 'application/xml', 'application/x-msexcel', 'application/x-rar', 'application/x-rar-compressed', 'application/x-rtf', 'application/x-shockwave-flash', 'application/x-sit', 'application/x-stuffit', 'application/x-troff-msvideo', 'application/x-zip', 'application/x-zip-compressed', 'application/zip', 'audio/*', 'image/*', 'multipart/x-gzip', 'multipart/x-zip', 'text/plain', 'text/rtf', 'text/richtext', 'text/xml', 'video/*' ); $this->mime_types = array( 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'flv' => 'video/x-flv', 'js' => 'application/x-javascript', 'json' => 'application/json', 'tiff' => 'image/tiff', 'css' => 'text/css', 'xml' => 'application/xml', 'doc' => 'application/msword', 'docx' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'xlt' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xld' => 'application/vnd.ms-excel', 'xla' => 'application/vnd.ms-excel', 'xlc' => 'application/vnd.ms-excel', 'xlw' => 'application/vnd.ms-excel', 'xll' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'rtf' => 'application/rtf', 'pdf' => 'application/pdf', 'html' => 'text/html', 'htm' => 'text/html', 'php' => 'text/html', 'txt' => 'text/plain', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'mp3' => 'audio/mpeg3', 'wav' => 'audio/wav', 'aiff' => 'audio/aiff', 'aif' => 'audio/aiff', 'avi' => 'video/msvideo', 'wmv' => 'video/x-ms-wmv', 'mov' => 'video/quicktime', 'zip' => 'application/zip', 'tar' => 'application/x-tar', 'swf' => 'application/x-shockwave-flash', 'odt' => 'application/vnd.oasis.opendocument.text', 'ott' => 'application/vnd.oasis.opendocument.text-template', 'oth' => 'application/vnd.oasis.opendocument.text-web', 'odm' => 'application/vnd.oasis.opendocument.text-master', 'odg' => 'application/vnd.oasis.opendocument.graphics', 'otg' => 'application/vnd.oasis.opendocument.graphics-template', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'otp' => 'application/vnd.oasis.opendocument.presentation-template', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template', 'odc' => 'application/vnd.oasis.opendocument.chart', 'odf' => 'application/vnd.oasis.opendocument.formula', 'odb' => 'application/vnd.oasis.opendocument.database', 'odi' => 'application/vnd.oasis.opendocument.image', 'oxt' => 'application/vnd.openofficeorg.extension', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docm' => 'application/vnd.ms-word.document.macroEnabled.12', 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', 'thmx' => 'application/vnd.ms-officetheme', 'onetoc' => 'application/onenote', 'onetoc2' => 'application/onenote', 'onetmp' => 'application/onenote', 'onepkg' => 'application/onenote', |
|