我想在我的网站上为图片创建一个上传功能,一旦图片上传,我想查看我网站内的图片。
我看了这个Youtube并从开始他的代码写了但是由于某种原因,当我尝试上传图像时它似乎工作但它根本不会上传,而且我的托管服务的PHP管理员内部( one.com)我得到这条消息“MySQL返回一个空结果集(即零行)。(查询耗时0.0004秒)”顺便说一句,我把连接参数保留为(localhost,root和“”)但实际上是代码它们对我的数据库是正确的。
代码:
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<DOCTYPE html!>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header class="main-header">
<div class="search">
<div class="logo">
<img src="img/nklogo.png"></div>
<div class="searchme">
<form><input type="search" id="name" name="name" placeholder="Search" autofocus autocomplete="on">
</form>
</div>
</div>
<div class="main-content">
<div class="primary col">
<h1>Today Jobs</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="secondary col">
<h1>Upcoming Jobs</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Send"/>
</div>
</div>
<div class="third col">
<h1>To-do list</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Send"/>
</div>
</div>
</div>
<div class="sub-content">
<div class="primary col">
<h1>Draw</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="secondary col">
<h1>Sync</h1>
<img src="img/database.png">
<p>Sync now</p>
<div class="button">
<input type="button"name="send" value="Sync"/>
</div>
</div>
<div class="third col">
<h1>Backup</h1>
<img src="img/database.png">
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="image" />
<br/><br/>
<div class="button">
<input type="submit"name="submit" value="Upload"/>
</div>
</form>
<?php
if(isset($_POST['submit'])){
if(getimagesize($_FILES['image']['tmp_name'])){
echo "Please select an image.";
}
else{
$image=addslashes($_FILES['image']['tmp_name']);
$name=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image=base64_encode($image);
saveimage($name,$image);
}
}
displayimage();
function saveimage($name,$image){
$mysql_hostname="localhost";
$mysql_user="root";
$mysql_password="";
$mysql_database="";
$bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or die("Bad Connection");
mysql_select_db($mysql_database,$bd)or die("Bad Connection");
$qry="insert into images (name,image) values ('$name','$image')";
$result=mysql_query($qry,$bd);
if($result){
echo "<br/>Image Uploaded.";
}
else{
echo "<br/>Image not Uploaded.";
}
}
function displayimage(){
$mysql_hostname="localhost";
$mysql_user="root";
$mysql_password="";
$mysql_database="";
$bd=mysql_connect($mysql_hostname,$mysql_user,$mysql_password)or die("Bad Connection");
mysql_select_db($mysql_database,$bd)or die("Bad Connection");
$qry="select * from images";
$result=mysql_query($qry,$bd);
while($row = mysql_fetch_array($result)){
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
}
mysql_close($bd);
}
?>
</div>
</div>
</header>
</body>
</html>