我不知道为什么这不起作用。请帮助我们来吧
<form action="login.php" method="post" enctype="multipart/form-data">
<br><br><br>
Select image: <input type="file" name="image" size="40" id="image">
<br><small> must be less than 512kb </small>
<br><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
数据库中有3列,如id,name,image为long blob type
<?php
if(isset($_POST['submit']))
{
$con=@mysqli_connect("localhost","root","","work");
$imagedata=mysqli_real_escape_string($con,file_get_contents($_FILES['image'] ['tmp_name']));
$imagename=mysqli_real_escape_string($con,$_FILES['image'] ['name']);
$imagetype=mysqli_real_escape_string($con,$_FILES['image'] ['type']);
if(substr($imagetype,0,5) =="image")
{
$con=@mysqli_connect("localhost","root","","work");
mysqli_query($con,"INSERT INTO pics VALUES('$imagename','$imagedata')" or die (mysqli_error($con)));
}
else
{
echo"only images are allowed";
}
}
?>
<img src="showimage.php" />
showimage.php
<?php
$con=@mysqli_connect("localhost","root","","work");
$query = mysqli_query($con,"select* from pics" or die (mysqli_error($con)));
$row=mysqli_fetch_assoc($query);
$imagedata= $row["image"];
header("content-type: image/jpeg");
echo $imagedata;
?>
请帮帮我们。我不知道我在这里做错了什么。
答案 0 :(得分:2)
$con
?mysqli_real_escape_string()
有助于清理变量的值,但最好也必须使用mysqli_prepared statement。images
accept
标记作为输入文件。将此accept=".jpg, .jpeg, .png"
添加到您的<input type="file"..
。并在PHP提交的页面中添加一个检查,以检查它是否确实是一个图像。您可以将login.php更改为:
<?php
if(isset($_POST['submit']))
{
$con = new mysqli("localhost","root","","work"); /* ESTABLISH CONNECTION */
$uploadedfile = $_FILES["image"]["tmp_name"];
$allowedExts = array("png","jpg","jpeg"); /* ACCEPTED FILE FORMAT */
$filename = $_FILES["image"]["name"]; /* NAME OF THE FILE */
$extension = pathinfo($filename, PATHINFO_EXTENSION); /* GET THE FILE EXTENSION */
$extension = strtolower($extension); /* LOWER THE STRINGS OF THE EXTENSION */
if(in_array($extension,$allowedExts)){ /* IF FILE IS INDEED AN IMAGE */
$path = "images/".$filename; /* DIRECTORY WHERE YOU WANT TO STORE THE IMAGE ALONG WITH THE FILE NAME */
move_uploaded_file($uploadedfile,$path); /* MOVE THE FILE TO YOUR IMAGES FOLDER */
/* PLEASE CHANGE THE NECESSARY TABLE NAME AND COLUMN NAME IN THE QUERY BELOW*/
if($stmt = $con->prepare("INSERT INTO pics (name) VALUES (?)")){
$stmt->bind_param("s",$filename); /* BIND THE FILE NAME TO THE QUERY */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->close();
} /* END OF PREPARED STATEMENT */
echo '<img src="images/'.$filename.'">'; /* OUTPUT THE UPLOADED IMAGE */
} /* END OF IF; CHECKING THE ALLOWED EXTENSIONS */
else { /* IF FILE FORMAT IS NOT SUPPORTED */
echo "You did not upload an image.";
} /* END OF ELSE */
} /* END OF ISSET POST SUBMIT */
?>
注意:我没有重命名文件名,因此如果用户上传的文件与前一个文件具有相同的文件名,则会导致冲突。在继续renaming of files之前先了解此代码。