blob_main.html
<html>
<head>
<title>
</title>
</head>
<body>
<h1>Upload new files</h1>
<form action="blob_db.php" method="post" enctype="multipart/form-data">
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<label for="userfile">Upload a file:</label>
<input type="file" name="userfile" id="userfile" />
<input type="submit" value="Send File" />
</div>
</form>
</body>
</html>
blob_db.php
<?
$clear = array();
$clear['tmp_name'] = $_FILES['userfile']['tmp_name'];
$clear['name'] = $_FILES['userfile']['name'];
$clear['size'] = $_FILES['userfile']['size'];
$clear['type'] = $_FILES['userfile']['type'];
$clear['error'] = $_FILES['userfile']['error'];
if( basename($clear['name']) != $clear['name'] ){
echo "fatal error. forbidden file name <br />";
exit;
}
echo $clear['tmp_name'].'<br />';
echo $clear['name'].'<br />';
echo $clear['size'].'<br />';
echo $clear['type'].'<br />';
if( $clear['error'] > 0 ){
echo "error code = [".$clear['error']."]<br />";
}
$db = mysqli_connect('localhost', user, passwd, dbname);
if (!$db) {
echo "Connect failed: " . mysqli_connect_error();
exit();
}
$name = $clear['name'];
$upload_dir = $_SERVER['DOCUMENT_ROOT'].'/upload';
//echo $upload_dir.'/'.$name.'<br />';
//echo nl2br(`ls /tmp -al`);
if( is_uploaded_file($clear['tmp_name'])){
if( move_uploaded_file($clear['tmp_name'], "$upload_dir/$name") ){
echo 'move_uploaded_file succeed <br />';
}
else{
echo 'move_uploaded_file fail <br />';
}
}
//$fp = fopen("$upload_dir/$name", "rb");
//$content = addslashes(fread($fp, filesize("$upload_dir/$name")));
$fname = $upload_dir.'/'.$name;
//fclose($fp);
$query = "INSERT INTO blob_test(name, content) VALUES('$name', '". mysql_escape_string(file_get_contents($fname)) ."');";
$result = mysqli_query($db, $query) or die('fail to query');
mysqli_free_result($result);
mysqli_close($db);
?>
mysql schema
CREATE TABLE IF NOT EXISTS `blob_test` ( `name` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL, `content` blob, `key` tinyint(4) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
'php' 카테고리의 다른 글
php password md5 hash (0) | 2013.02.02 |
---|---|
mongodb + rockmongo with php in ubuntu (0) | 2013.01.08 |
PHP와 MySQL을 이용한 웹 프로그래밍 (0) | 2013.01.01 |
xml to json 변환 (0) | 2012.12.20 |
sms 인증보내기 php 소스 (0) | 2012.12.19 |