我制作了一个页面,您可以在其中上传文件,该文件将被存储到数据库中,但是它不起作用,而且我不知道为什么会这样。这是我的代码:
HTML
<form id="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
PHP(在同一页面上)
<?php
if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)
{
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$parser = fopen($file_tmp, 'r');
$content = fread($parser, filesize($file_tmp));
$content = addslashes($content);
fclose($parser);
if(!get_magic_quotes_gpc())
{
$file_name = addcslashes($file_name);
}
include('config.php');
$conn->query("SET NAMES utf8");
$query="INSERT INTO files (name, size, type, content, status)"."VALUES (:name, :size, :type, :content, 'Iedereen') ";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':name', $file_name);
$stmt->bindParam(':size', $file_size);
$stmt->bindParam(':type', $file_type);
$stmt->bindParam(':content', $content);
$stmt->execute();
}
?>
答案 0 :(得分:-1)
这是我轻松地将图像上传到称为Realestate的数据库的方式...只需遵循代码并更改字段名,表名和数据库名。
<form class="form" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="submit" value="Submit" class="btn btn-primary btn-lg">update</button>
</form>
<?php
if (isset($_POST['submit'])) {
$filename = $_FILES["file"]["name"];
$filetmp = $_FILES["file"]["tmp_name"];
// rename image
$div = explode('.', $filename);
$file_ext = strtolower(end($div));
$unique_name = substr(md5(time()), 0,10).'.'.$file_ext;
$path = "images/".$unique_name;
move_uploaded_file($filetmp,$path);
$query = "INSERT INTO files(name,path) VALUES
('$filename','$path')";
$result = mysqli_query($conn,$query);
if (mysqli_query($conn,$query)) {
echo "Records updated successfully.";
} else{
echo "ERROR: Could not able to execute $delete. " . mysqli_error($conn);
}
}
?>
$conn is my database connection variable.