Php 将文件路径上载到数据库
我试图从一个输入上传两个文件,两个文件都显示在uploads文件夹中,但只有一个文件路径进入数据库 例如,如果我上传test1.pdf和test2.pdf,它们都将进入uploads文件夹,但test1.pdf将插入数据库的两列,而不是一列 在我发疯之前,有人能帮我吗Php 将文件路径上载到数据库,php,mysql,Php,Mysql,我试图从一个输入上传两个文件,两个文件都显示在uploads文件夹中,但只有一个文件路径进入数据库 例如,如果我上传test1.pdf和test2.pdf,它们都将进入uploads文件夹,但test1.pdf将插入数据库的两列,而不是一列 在我发疯之前,有人能帮我吗 if (count($_FILES['field2']['name']) >= 1) { //Loop through each file for ($i = 0; $i < count($_FI
if (count($_FILES['field2']['name']) >= 1) {
//Loop through each file
for ($i = 0; $i < count($_FILES['field2']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['field2']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//save the filename
$shortname = $_FILES['field2']['name'][$i];
//save the url and the file
$filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['field2']['name'][$i];
$filePath1 = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['field2']['name'][$i];
//Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $filePath)) {
$sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs (field1, field2)
VALUES
('$filePath','$filePath1')";
mysql_query($sql);
}
}
}
}
}
if(计数($\u文件['field2']['name'])>=1){
//循环浏览每个文件
对于($i=0;$i
如果要使用相同的输入文件上载两个或多个文件,请更新代码。在此代码中保存文件名和路径。一次,使用循环无法同时获取两个文件路径
1。重要事项删除mysql并使用pdo或mysqli
<if (count($_FILES['enviroupload']['name']) >= 1) {
//Loop through each file
for ($i = 0; $i < count($_FILES['enviroupload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['enviroupload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//save the filename
$shortname = $_FILES['enviroupload']['name'][$i];
//save the url and the file
$filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['enviroupload']['name'][$i];
//Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $filePath)) {
$sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs (field1, field2)
VALUES
('$shortname','$filePath')";
//mysql_query($sql);
echo $sql;
echo "<br>";
}
}
}
=1){
//循环浏览每个文件
对于($i=0;$i”;
}
}
}
}
在此代码中,文件路径和文件名将保存在db中,如果您只想保存文件路径,则需要更新代码为什么要使用mysql*函数它在php的较新版本中已被弃用,在PHP7中已被删除。您需要一个循环。@RossH如果您不在乎,为什么还要开发?@Script47-您能告诉我如何操作吗?您只需上载文件路径,在这种情况下,如果(移动上传的文件($tmpFilePath,$filePath)){这不是只上传一个文件吗?这和我现在遇到的问题是一样的。我需要一次上传多个文件。评论不用于扩展讨论或调试会话,我们不鼓励用户从此网站上进行交互。其他信息应编辑到问题或答案中。
<if (count($_FILES['enviroupload']['name']) >= 1) {
//Loop through each file
for ($i = 0; $i < count($_FILES['enviroupload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['enviroupload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//save the filename
$shortname = $_FILES['enviroupload']['name'][$i];
//save the url and the file
$filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['enviroupload']['name'][$i];
//Upload the file into the temp dir
if (move_uploaded_file($tmpFilePath, $filePath)) {
$sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs (field1, field2)
VALUES
('$shortname','$filePath')";
//mysql_query($sql);
echo $sql;
echo "<br>";
}
}
}