PHP导出数据库方法

定义一个db_dump函数如下:

<?PHP
function db_dump($host,$user,$pwd,$db) {
    $mysqlconlink = mysql_connect($host,$user,$pwd , true);
    if (!$mysqlconlink)
        echo sprintf('No MySQL connection: %s',mysql_error())."<br/>";
    mysql_set_charset( 'utf8', $mysqlconlink );
    $mysqldblink = mysql_select_db($db,$mysqlconlink);
    if (!$mysqldblink)
        echo sprintf('No MySQL connection to database: %s',mysql_error())."<br/>";
    $tabelstobackup=array();
    $result=mysql_query("SHOW TABLES FROM `$db`");
    if (!$result)
        echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
    while ($data = mysql_fetch_row($result)) {
            $tabelstobackup[]=$data[0];
    }
    if (count($tabelstobackup)>0) {
        $result=mysql_query("SHOW TABLE STATUS FROM `$db`");
        if (!$result)
            echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
        while ($data = mysql_fetch_assoc($result)) {
            $status[$data['Name']]=$data;
        }
        if ($file = fopen("$db.sql", 'wb')) {
            fwrite($file, "-- ---------------------------------------------------------\n");
            fwrite($file, "-- Database Name: $db\n");
            fwrite($file, "-- ---------------------------------------------------------\n\n");
            fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n");
            fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n");
            fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n");
            fwrite($file, "/*!40101 SET NAMES '".mysql_client_encoding()."' */;\n");
            fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n");
            fwrite($file, "/*!40103 SET TIME_ZONE='".mysql_result(mysql_query("SELECT @@time_zone"),0)."' */;\n");
            fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n");
            fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n");
            fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n");
            fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n");
            foreach($tabelstobackup as $table) {
                echo sprintf('Dump database table "%s"',$table)."<br/>";
                need_free_memory(($status[$table]['Data_length']+$status[$table]['Index_length'])*3);
                _db_dump_table($table,$status[$table],$file);
            }
            fwrite($file, "\n");
            fwrite($file, "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n");
            fwrite($file, "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n");
            fwrite($file, "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n");
            fwrite($file, "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n");
            fwrite($file, "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n");
            fwrite($file, "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n");
            fwrite($file, "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
            fwrite($file, "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n");
            fclose($file);
            echo 'Database dump done!'."<br/>";
        } else {
            echo 'Can not create database dump!'."<br/>";
        }
    } else {
        echo 'No tables to dump'."<br/>";
    }
}
function _db_dump_table($table,$status,$file) {
    fwrite($file, "\n");
    fwrite($file, "--\n");
    fwrite($file, "-- Table structure for table $table\n");
    fwrite($file, "--\n\n");
    fwrite($file, "DROP TABLE IF EXISTS `" . $table .  "`;\n");
    fwrite($file, "/*!40101 SET @saved_cs_client     = @@character_set_client */;\n");
    fwrite($file, "/*!40101 SET character_set_client = '".mysql_client_encoding()."' */;\n");
    $result=mysql_query("SHOW CREATE TABLE `".$table."`");
    if (!$result) {
        echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW CREATE TABLE `".$table."`")."<br/>";
        return false;
    }
    $tablestruc=mysql_fetch_assoc($result);
    fwrite($file, $tablestruc['Create Table'].";\n");
    fwrite($file, "/*!40101 SET character_set_client = @saved_cs_client */;\n");
    $result=mysql_query("SELECT * FROM `".$table."`");
    if (!$result) {
        echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SELECT * FROM `".$table."`")."<br/>";
        return false;
    }
    fwrite($file, "--\n");
    fwrite($file, "-- Dumping data for table $table\n");
    fwrite($file, "--\n\n");
    if ($status['Engine']=='MyISAM')
        fwrite($file, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
    while ($data = mysql_fetch_assoc($result)) {
        $keys = array();
        $values = array();
        foreach($data as $key => $value) {
            if($value === NULL)
                $value = "NULL";
            elseif($value === "" or $value === false)
                $value = "''";
            elseif(!is_numeric($value))
                $value = "'".mysql_real_escape_string($value)."'";
            $values[] = $value;
        }
        fwrite($file, "INSERT INTO `".$table."` VALUES ( ".implode(", ",$values)." );\n");
    }
    if ($status['Engine']=='MyISAM')
        fwrite($file, "/*!40000 ALTER TABLE ".$table." ENABLE KEYS */;\n");
}
function need_free_memory($memneed) {
    if (!function_exists('memory_get_usage'))
        return;
    $needmemory=@memory_get_usage(true)+inbytes($memneed);
    if ($needmemory>inbytes(ini_get('memory_limit'))) {
        $newmemory=round($needmemory/1024/1024)+1 .'M';
        if ($needmemory>=1073741824)
            $newmemory=round($needmemory/1024/1024/1024) .'G';
        if ($oldmem=@ini_set('memory_limit', $newmemory))
            echo sprintf(__('Memory increased from %1$s to %2$s','backwpup'),$oldmem,@ini_get('memory_limit'))."<br/>";
        else
            echo sprintf(__('Can not increase memory limit is %1$s','backwpup'),@ini_get('memory_limit'))."<br/>";
    }
}
function inbytes($value) {
    $multi=strtoupper(substr(trim($value),-1));
    $bytes=abs(intval(trim($value)));
    if ($multi=='G')
        $bytes=$bytes*1024*1024*1024;
    if ($multi=='M')
        $bytes=$bytes*1024*1024;
    if ($multi=='K')
        $bytes=$bytes*1024;
    return $bytes;
}
?>

使用方法:

db_dump('数据库服务器', '数据库用户名', '数据库密码', '数据库名');

转自:http://www.cnblogs.com/flying_bat/p/3324793.html

php按指定元素值去除数组元素的实现方法

<?php 
    //去除值为"Cat"的元素 
    $a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); 
    print_r($a); 
    unset($a[array_search("Cat",$a)]);//array_search("Cat",$a)按元素值返回键名。去除后保持索引 
    print_r($a); 
    
    //或
    $arr = array();
    foreach ($a as $key=>$value){
       if ($value != 'Cat') $arr[] = $value;
    }
    
    var_dump($arr);
    
?> 

显示结果

//去除前: 
Array ( 
    [a] => Dog 
    [b] => Cat 
    [c] => Horse 
) 

//去除后: 
Array ( 
    [a] => Dog 
    [c] => Horse 
)

linux批量删除网页挂马

新建个.sh文件 (sfcq、cqsf、zhaosf...是目录名)

#!/bin/sh
#comments

find /home/wwwroot/ -name sfcq | xargs rm -rf
find /home/wwwroot/ -name cqsf | xargs rm -rf
find /home/wwwroot/ -name zhaosf | xargs rm -rf
find /home/wwwroot/ -name kaisf | xargs rm -rf
find /home/wwwroot/ -name haosf | xargs rm -rf
find /home/wwwroot/ -name qusf | xargs rm -rf
find /home/wwwroot/ -name kage | xargs rm -rf
find /home/wwwroot/ -name apage | xargs rm -rf
find /home/wwwroot/ -name wansf | xargs rm -rf  

添加个Crontab任务,OK

比起这种方法更建议大家去做好服务器安全~

粘贴上传图片实现

HTML:

<div id="box" style="width:200px;height:200px;border:1px solid;" contenteditable></div>
<script>
    document.querySelector('#box').addEventListener('paste', function(e) {
    // chrome
    if (e.clipboardData && e.clipboardData.items[0].type.indexOf('image') > -1) {
        var that = this,
            reader = new FileReader();
            file = e.clipboardData.items[0].getAsFile();

        reader.onload = function(e) {
            var xhr = new XMLHttpRequest(),
                fd = new FormData();

            xhr.open('POST', '../upload.php', true);
            xhr.onload = function () {
                var img = new Image();
                img.src = xhr.responseText;
                that.appendChild(img);  // 这里是把上传后得到的地址插入到#box里
            }

            fd.append('file', this.result); // this.result得到图片的base64
            xhr.send(fd);
        }
        reader.readAsDataURL(file);
    }
}, false);
</script>

PHP:

<?php
    header("Access-Control-Allow-Origin:*");
    $url = 'http://'.$_SERVER['HTTP_HOST'];
    $file = $_POST["file"];
    $data = base64_decode(str_replace('data:image/png;base64,', '', $file));  //截图得到的只能是png格式图片,所以只要处理png就行了
    $name = md5(time()) . '.png';  // 这里把文件名做了md5处理
    file_put_contents($name, $data);
    echo "$url/$name";
?>

Chrome桌面提醒功能,兼容新老版本,兼容firefox

//桌面提醒
function notify(title, content) {
        
        if(!title && !content){
            title = "桌面提醒";
            content = "您看到此条信息桌面提醒设置成功";
        }
        var iconUrl = "/images/send_ok.png";
        
        if (window.webkitNotifications) {
            //chrome老版本
            if (window.webkitNotifications.checkPermission() == 0) {
                var notif = window.webkitNotifications.createNotification(iconUrl, title, content);
                notif.display = function() {}
                notif.onerror = function() {}
                notif.onclose = function() {}
                notif.onclick = function() {this.cancel();}
                notif.replaceId = 'Meteoric';
                notif.show();
            } else {
                window.webkitNotifications.requestPermission($jy.notify);
            }
        }
        else if("Notification" in window){
            // 判断是否有权限
            if (Notification.permission === "granted") {
                var notification = new Notification(title, {
                    "icon": iconUrl,
                    "body": content,
                });
            }
            //如果没权限,则请求权限
            else if (Notification.permission !== 'denied') {
                Notification.requestPermission(function(permission) {
                    // Whatever the user answers, we make sure we store the
                    // information
                    if (!('permission' in Notification)) {
                        Notification.permission = permission;
                    }
                    //如果接受请求
                    if (permission === "granted") {
                        var notification = new Notification(title, {
                            "icon": iconUrl,
                            "body": content,
                        });
                    }
                });
            }
        }
    }

ps: Chrome需在服务器环境测试

转自:http://blog.csdn.net/qzmrock/article/details/39802391