LinuxЧÀÍÆ÷·ÀÓù£º±£»¤Web½Ó¿ÚÃâÊܶñÒâÎļþÉÏ´«¹¥»÷¡£
LinuxЧÀÍÆ÷·ÀÓù£º±£»¤Web½Ó¿ÚÃâÊܶñÒâÎļþÉÏ´«¹¥»÷
½üÄêÀ´£¬Ëæ×ÅÍøÂçµÄÆÕ¼°ºÍÉú³¤£¬WebÓ¦ÓóÌÐòµÄʹÓÃÔ½À´Ô½Æձ顣Ȼ¶ø£¬ÓëÖ®Åãͬ¶øÀ´µÄÊÇÖÖÖÖÇå¾²Íþв£¬ÆäÖÐÖ®Ò»¾ÍÊǶñÒâÎļþÉÏ´«¹¥»÷¡£¶ñÒâÎļþÉÏ´«¹¥»÷ÊÇÖ¸¹¥»÷ÕßÏòЧÀÍÆ÷ÉÏ´«°üÀ¨¶ñÒâ´úÂëµÄÎļþ£¬´Ó¶ø»ñȡЧÀÍÆ÷ȨÏÞ»òÕßÈö²¥¶ñÒâÄÚÈÝ¡£
ΪÁ˱£»¤Web½Ó¿ÚÃâÊܶñÒâÎļþÉÏ´«¹¥»÷£¬ÎÒÃÇ¿ÉÒÔ½ÓÄÉһЩÓÐÓõķÀÓù²½·¥¡£ÏÂÃ潫ÏÈÈÝһЩ³£ÓõķÀÓùÒªÁì²¢ÌṩÏà¹Ø´úÂëʾÀý¡£
ÎļþÀàÐͼì²é
Ê×ÏÈ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý¼ì²éÉÏ´«ÎļþµÄÎļþÀàÐÍÀ´¹ýÂ˶ñÒâÎļþ¡£ÔÚЧÀÍÆ÷¶Ë£¬ÎÒÃÇ¿ÉÒÔʹÓÃFileinfoÀ©Õ¹»òÕßmime_content_type()º¯ÊýÀ´»ñÈ¡ÉÏ´«ÎļþµÄMIMEÀàÐÍ¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔÓë°×Ãûµ¥¾ÙÐнÏÁ¿£¬Ö»ÔÊÐíÌض¨µÄÎļþÀàÐÍÉÏ´«£¬ÆäËûÀàÐ͵ÄÎļþ½«±»¾Ü¾ø¡£
ʾÀý´úÂ룺
<?php $allowedTypes = array('image/jpeg', 'image/png', 'image/gif'); $uploadedFile = $_FILES['file']; if (in_array(mime_content_type($uploadedFile['tmp_name']), $allowedTypes)) { // ÔÊÐíÎļþÉÏ´« } else { // ¾Ü¾øÎļþÉÏ´« } ?>
µÇ¼ºó¸´ÖÆ
ÎļþÃû¼ì²é
³ýÁËÎļþÀàÐͼì²é£¬ÎÒÃÇ»¹¿ÉÒÔ¶ÔÉÏ´«µÄÎļþÃû¾ÙÐмì²é¡£¹¥»÷Õß¾³£Ê¹ÓþßÓÐαװÐÔµÄÎļþÃûÀ´ÓÕÆЧÀÍÆ÷¡£ÀýÈ磬¹¥»÷Õß¿ÉÒÔ½«shell.phpÎļþÖØÃüÃûΪshell.jpgÀ´ÈƹýÎļþÀàÐͼì²é¡£Òò´Ë£¬ÎÒÃÇÐèÒª¼ì²éÎļþÃûµÄÀ©Õ¹ÃûÊÇ·ñÓëÎļþÀàÐÍÏàÆ¥Åä¡£
ʾÀý´úÂ룺
<?php $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif'); $uploadedFile = $_FILES['file']; $fileInfo = pathinfo($uploadedFile['name']); if (in_array(strtolower($fileInfo['extension']), $allowedExtensions)) { // ÔÊÐíÎļþÉÏ´« } else { // ¾Ü¾øÎļþÉÏ´« } ?>
µÇ¼ºó¸´ÖÆ
Îļþ¾ÞϸÏÞÖÆ
ÁíÍ⣬ÎÒÃÇ»¹¿ÉÒÔÏÞÖÆÉÏ´«ÎļþµÄ¾Þϸ£¬ÒÔ±ÜÃâ¹¥»÷ÕßÉÏ´«¹ý´óµÄÎļþÏûºÄЧÀÍÆ÷×ÊÔ´»òÕßÔì³É¾Ü¾øЧÀÍ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýPHPµÄini_set()º¯ÊýÀ´ÐÞ¸Äphp.iniÉèÖÃÎļþÖеÄÉÏ´«Îļþ¾ÞϸÏÞÖÆ¡£
ʾÀý´úÂ룺
<?php ini_set('upload_max_filesize', '2M'); ini_set('post_max_size', '2M'); ?>
µÇ¼ºó¸´ÖÆ
Îļþ´æ´¢Î»ÖÃ
×îºó£¬ÎªÁ˱£»¤Ð§ÀÍÆ÷ÃâÊܹ¥»÷£¬ÎÒÃÇÐèÒª½«ÉÏ´«µÄÎļþ´æ´¢ÔÚÇå¾²µÄλÖá£Ê×ÏÈ£¬ÎÒÃÇÓ¦¸Ã½«Îļþ´æ´¢ÔÚЧÀÍÆ÷¸ùĿ¼֮Í⣬ÒÔ±ÜÃâ¹¥»÷ÕßÖ±½Ó»á¼ûÉÏ´«µÄÎļþ¡£Æä´Î£¬ÎÒÃÇ¿ÉÒÔÔÚÎļþ´æ´¢Â·¾¶ÖÐʹÓÃËæ»ú×Ö·û´®»òÕßÉ¢ÁÐÖµ£¬ÒÔÔöÌíÎļþ·¾¶µÄÍƲâÄѶȡ£
ʾÀý´úÂ룺
<?php $uploadedFile = $_FILES['file']; $targetDirectory = '/path/to/uploads/'; $targetFileName = md5(uniqid()) . '-' . basename($uploadedFile['name']); $targetPath = $targetDirectory . $targetFileName; if (move_uploaded_file($uploadedFile['tmp_name'], $targetPath)) { // ÎļþÉÏ´«ÀÖ³É } else { // ÎļþÉÏ´«Ê§°Ü } ?>
µÇ¼ºó¸´ÖÆ
×ܽ᣺
¶ñÒâÎļþÉÏ´«¹¥»÷¶ÔЧÀÍÆ÷Çå¾²×é³ÉÁËÑÏÖØÍþв¡£ÎªÁ˱£»¤Web½Ó¿ÚÃâÊܸù¥»÷£¬ÎÒÃÇ¿ÉÒÔ½ÓÄÉһϵÁзÀÓù²½·¥£¬°üÀ¨ÎļþÀàÐͼì²é¡¢ÎļþÃû¼ì²é¡¢Îļþ¾ÞϸÏÞÖÆÒÔ¼°Îļþ´æ´¢Î»ÖõĺÏÀíÉèÖá£
È»¶ø£¬½öÒÀÀµÕâЩ·ÀÓù²½·¥²»¿É°ü¹Ü¾ø¶ÔÇå¾²¡£Òò´Ë£¬ÎÒÃÇ»¹Ó¦°´ÆÚ¸üÐÂЧÀÍÆ÷Èí¼þ¡¢¼à¿ØЧÀÍÆ÷ÈÕÖ¾¡¢ÊµÊ±ÐÞ¸´Îó²îµÈ£¬ÒÔ¼á³ÖЧÀÍÆ÷µÄÇå¾²ÐÔ¡£
ͨ¹ýÓÐÓõķÀÓùÊֶκÍÓÅÒìµÄÇ徲ʵ¼ù£¬ÎÒÃÇ¿ÉÒÔ×îºéÁ÷ƽµØ±£»¤Web½Ó¿Ú²»ÊܶñÒâÎļþÉÏ´«¹¥»÷µÄÍþв¡£
ÒÔÉϾÍÊÇLinuxЧÀÍÆ÷·ÀÓù£º±£»¤Web½Ó¿ÚÃâÊܶñÒâÎļþÉÏ´«¹¥»÷¡£µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡