我是编程新手,不太擅长编程, 我想用人名创建一个from,预览图像并存储在数据库中(MYSQL)
到目前为止,我可以保存到人名,文件名,文件类型和大小的数据库.OK。 图像存储到文件夹也是.OK。
问题是在用户点击上传之前我无法预览图像 另一个问题我不想显示所有上传的数组只有我当前的上传。
dbtuts
-
tbl_uploads
CREATE TABLE IF NOT EXISTS `tbl_uploads` (
`id` int(10) NOT NULL,
`name` varchar(255) NOT NULL,
`file` varchar(100) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_uploads`
--
INSERT INTO `tbl_uploads` (`id`, `name`, `file`, `type`, `size`) VALUES
(1, '', '70455-(r)11d.jpg', 'image/jpeg', 19),
(2, '', '77190-3d-glass-green-effect.jpg', 'image/jpeg', 249),
(3, '', '85477-123.jpg', 'image/jpeg', 19),
(4, '', '12982-123.jpg', 'image/jpeg', 19),
(5, '', '35705-123.jpg', 'image/jpeg', 19),
(6, '', '48313-123.jpg', 'image/jpeg', 19),
(7, '', '28810-123.jpg', 'image/jpeg', 19),
(8, '', '67467-123.jpg', 'image/jpeg', 19),
(9, 'dddd', '80982-123.jpg', 'image/jpeg', 19);
dbconfig.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "dbtuts";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
?>
的index.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql(Test)</label>
</div>
<div id="body">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
<?php
if(isset($_GET['success']))
{
include 'view.php';
?>
<label>File Uploaded Successfully... </label>
<!-- <a href="view.php">click here to view file.</a> -->
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
<?php
}
?>
</div>
</body>
</html>
view.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th>
</tr>
<tr>
<td>name</td>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT * FROM tbl_uploads";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['name']?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
upload.php的
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
$name=$_POST['name'];
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(name,file,type,size) VALUES('$name','$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>