这里我将单个文件上传到数据库并且我使用了longblob,如何更改此代码以将多个文件上传到具有相同ID的数据库,并且我还能够下载多个上传的文件以获得相同的ID下载选项。有人可以帮帮我吗?
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '12345', 'documentksrsac');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
//$project_name = $_POST["pname"];
//$project_name = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['project_name']));
// $project_name =real_escape_string(['uploaded_file']['project_name']);
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `fileupload1` (
`name`, `mime`, `size`, `data`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}'
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// close connection
//mysqli_close($link);
?>
答案 0 :(得分:0)
对于多张图片上传,我使用的是给定代码: -
<form method="post" enctype="multipart/form-data">
<input type="file" name="img[]" >
<input type="file" name="img[]" >
<input type="file" name="img[]" >
<input type="file" name="img[]" >
<button type="submit" name="save" >Submit</button>
</form>
<?php
if(isset($_POST['save']))
{
$img_name=$_FILES['img']['name'];
$img_type=$_FILES['img']['type'];
$img_temp_name=$_FILES['img']['tmp_name'];
$img_size=$_FILES['img']['size'];
$count=count($img_name);
for($i=0; $i<$count; $i++){
$name=$img_name[$i];
$type=$img_type[$i];
$temp_name=$img_temp_name[$i];
$size=$img_size[$i];
$query = "INSERT INTO `fileupload1` (`name`, `mime`, `size`, `data`)VALUES ('{$name}', '{$type}', {$temp_name}, '{$size}')";
$result = $dbLink->query($query);
}
}