提问人:vit 提问时间:3/27/2017 更新时间:3/27/2017 访问量:133
将文件路径从数组上传到数据库
Upload file path from array to database
问:
我目前能够使用数组将多个单独的文件保存到文件夹中。现在,我想将文件夹中的各个文件路径添加到数据库中。
如何将每个文件的各个路径上传到文件夹?
从那里,我将如何将这些单独的路径插入到我的数据库中的单独字段中?
HTML格式:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<input type="submit" value="Upload all files">
</form>
</body>
</html>
PHP的:
<?php
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
$imageFileType = pathinfo($path,PATHINFO_EXTENSION);
$path = 'uploads/' . $_FILES['my-file']['name'];
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
$conn_info = "host=d port= dbname= user= password=";
$dbconn = pg_connect($conn_info)
or die('could not connect:' . pg_last_error());
echo"<br/>sucessful connection";
$query = "INSERT INTO photo2 (path) VALUES ('".$path."') ;";
$result = pg_query($dbconn, $query);
if (!$result) {
$errormessage = pg_last_error();
echo "<br/> error with query: " . $errormessage;
exit();
}
echo "<br/> file uploaded to PostgreSQL";
pg_close();
?>
答:
0赞
Ravinder Reddy
3/27/2017
#1
获取 for 循环中的路径。并且还在循环中编写插入查询。 数据库连接代码必须位于 for 循环之前。
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
// get path
$path = "uploads/".$name_array[$i];
echo $name_array[$i]." upload is complete<br>";
// Insert query comes here
$query = "INSERT INTO photo2 (path) VALUES ('".$path."') ;";
pg_query($dbconn, $query);
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
0赞
Jayanta
3/27/2017
#2
有您需要的完整代码。我想这可能会对你有所帮助,先生。
<?php
$conn_info = "host=d port= dbname= user= password=";
$dbconn = pg_connect($conn_info) or die('could not connect:' . pg_last_error());
echo "<br/>sucessful connection";
if(isset($_FILES['file_array'])){
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
// you don't need this lines of code
// $imageFileType = pathinfo($path, PATHINFO_EXTENSION);
// $path = 'uploads/' . $_FILES['my-file']['name'] . '.' . $imageFileType;
for($i=0;$i<count($tmp_name_array);$i++) {
$path = "uploads/" . $name_array[$i];
if(move_uploaded_file($tmp_name_array[$i], $path)){
$query = "INSERT INTO photo2 (path) VALUES ('".$path."');";
$result = pg_query($dbconn, $query);
if (!$result) {
$errormessage = pg_last_error();
echo "<br/> error with query: " . $errormessage;
exit();
} else {
echo $name_array[$i]." upload is complete<br>";
}
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
echo "<br/> file uploaded to PostgreSQL";
pg_close();
?>
评论