PHP文件上传 move_uploaded_file的使用

说明:
主要两个文件,很简单但是很有效,文件一可以分解为HTML和PHP文件
然后用PHP文件引用包含HTML文件

文件1:index.php

<?php
	header("Content-Type:text/html; charset=utf-8");
	include('../../../config/config.php');
	$conn = mysqli_connect($_config['db']['1']['dbhost'],$_config['db']['1']['dbuser'],$_config['db']['1']['dbpw']);
	if (! $conn) {
		die('无法连接数据库:' . mysqli_error());
	}
	echo "连接数据库成功。";

	echo "<pre>";
	print_r($_FILES);
	echo "</pre>";

	$tmpfile = $_FILES['file']['tmp_name'];
	$dstfile ="files/" . date('Y_m_d/') .$_FILES['file']['name'];
	$file_name_old = $_FILES['file']['name'];
	$file_type = $_FILES['file']['type'];
	$err_info = $_FILES['file']['error'];
	$file_size = ceil($_FILES['file']['size']/1024);

	// echo $file_name_old;
	// echo $file_type;
	// echo $err_info;
	// echo $file_size;
	// die();
	function crt_dir($path, $mode='0777'){
        if (!is_dir($path)) {
                mkdir($path, $mode, true);
        }
	}

	$dir_name = "files/" . date('Y_m_d/');

	crt_dir($dir_name);
	
	if (move_uploaded_file($tmpfile, $dstfile)){
		echo "sucess";
	}else{
		echo "failed";
	}

	mysqli_query($conn, "set names utf8");
	$sql = "insert into pic_to_pc(file_name,file_type,file_tmp_name,err_info,file_size,new_name) values('{$file_name_old}','{$file_type}','{$tmpfile}','{$err_info}','{$file_size}','{$dstfile}')";
	mysqli_select_db($conn,$_config['db']['1']['dbname']);
	mysqli_query($conn, $sql);
	// 下面这行可以删除
	mysqli_close($conn);

	echo "<br/>aa/plugin/iPhonePicToPC/toPic2.0/files/";
	echo "<label><br/><a href='./index.php'>here back</a></label>";

?>

文件2:upload.php

<!doctype html>

<html>
<head>
	<meta charset="UTF-8">
	<title>iPe Pic upload</title>
	<link rel="stylesheet" href="../../../static/bootstrap/bootstrap.min.css">
	<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
	<div class="container">
		<h2>Phone file upload</h2>
		<hr>
		<form role='form' action="upload.php" method="post" enctype="multipart/form-data">
			<div class="form-group">
				<label for='inputfile'>文件输入</label>
				<br /><br />
				<input class='btn btn-info btn-lg' type='file' name='file' id=''>
			</div>
			<br />
			<button type='submit' class='btn btn-default btn-lg'>提交</button>
		</form>
		<br />	
		<div class="table-responsive">
			<?php
			include('../../../config/config.php');
			$conn = mysqli_connect($_config['db']['1']['dbhost'],$_config['db']['1']['dbuser'],$_config['db']['1']['dbpw']);
			if (! $conn) {
				die('无法连接数据库:' . mysqli_error());
			}
			// echo "连接数据库成功。";

			mysqli_query($conn, "set names utf8");

			$sql = "select file_id, file_name, new_name from pic_to_pc ORDER BY file_id DESC LIMIT 0, 5";
			mysqli_select_db($conn,$_config['db']['1']['dbname']);

			$retval = mysqli_query($conn, $sql);
			if (! $retval) {
				die('无法读取数据:' . mysqli_error($conn));
			}

			// echo "<h2>数据库test中tl_url_log表中数据</h2>";
			echo "<table class='table table-striped table-bordered'> <thead> <tr> <th>ID</th> <th>NAME</th> </tr> </thead>";
			echo "<tbody>";

			while ($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
				echo "<tr><td>{$row['file_id']}</td>".
				"<td><a href='./{$row["new_name"]}'>{$row['file_name']}</a></td></tr>";

			}

			echo "</tbody> </table>";

			mysqli_close($conn);

			?>
		</div>
	</div>
</body>
</html>

需要在MySQL数据库中建立响应数据库和表字段

1.0版本 后续开发为复制粘贴到就可以用的插件模式 source/plugin

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值