我有这个文件上传代码,工作正常。我唯一的问题是当我上传多个文件时,它会在数据库中为每个文件创建多行。例如,如果我上传了5个文件,它将创建5个具有相同消息但具有不同文件名的新行。它不会绑定一条消息中的所有文件,也不会为数据库中包含多个文件的消息创建单行。
if(!empty($_FILES['files']['name'][0])){
$files = $_FILES['files'];
$uploaded = array();
$failed = array();
$allowed = array('jpg', 'jpeg','gif','png','txt', 'doc','docx', 'xls', 'xlsx', 'zip','rar','gz','7zip','ppt', 'pptx','rtf','pdf','svg','sav','csv');
foreach ($files['name'] as $position => $file_name) {
$file_temp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowed)){
if($file_error === 0){
if($file_size <= 20000000){
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = '../files/wcfiles/'.$file_name_new;
$file_destination = '../files/wcfiles/'.strtolower($file_name);
$file_db_path_array = array();
array_push($file_db_path_array, strtolower($file_name));
$file_db_path = 'msg_files/'. $file_db_path_array;
if(move_uploaded_file($file_temp, $file_destination)){
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload";}
} else {
$failed[$position] = "[{$file_name}] is too large.";}
} else {
$failed[$position] = "[{$file_name}] errored with code {$file_error}";}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";}
}
$ins_message = $conn->prepare("INSERT INTO conversation (msg_date, msg_id, msg_from, msg_to, msg_message, msg_files) VALUES (?,?,?,?,?,?)");
$ins_message->bind_param("ssssss", $msg_date, $msg_id, $msg_from, $msg_to, $msg_message, $file_db_path);
$ins_message->execute();
$ins_message->close();
文件代码
echo '<ul>';
foreach (explode(',', $msg_rows['msg_files']) as $file ) {
echo '<li><a href="../files/wcfiles/"'.$file.'>'.$file.'</a></li>';}
echo '</ul>';
答案 0 :(得分:2)
我认为这里最好的方法是将文件路径值推送到foreach
循环中的数组中。然后,在循环之后,将join
每个文件路径数组元素放入一个字符串中,每个字符串之间都有一个逗号,以便将完整的文件路径字符串插入到数据库中。
首先在foreach
循环之前实例化一个数组,以保存文件路径值。
$file_db_path_array = [];
foreach ($files['name'] as $position => $file_name) {
// ...
下一步将您的数据库插入代码移至foreach
循环关闭后:
} // end of foreach $files['name']
$ins_message = $conn->prepare("INSERT INTO conversation (msg_date, msg_id, msg_from, msg_to, msg_message, msg_files) VALUES (?,?,?,?,?,?)");
$ins_message->bind_param("ssssss", $msg_date, $msg_order_id, $msg_from, $msg_to, $msg_message, $file_db_path);
$ins_message->execute();
$ins_message->close();
然后将$file_db_path
分配行替换为:
array_push($file_db_path_array, 'msg_files/'.strtolower($file_name_new));
最后,在数据库插入代码中,将$file_db_path
更改为join(',', $file_db_path_array)
。
$ins_message->bind_param("ssssss", $msg_date, $msg_order_id, $msg_from, $msg_to, $msg_message, join(',', $file_db_path_array));
这里是完整的foreach
循环和数据库插入:
$file_db_path_array = [];
foreach ($files['name'] as $position => $file_name) {
$file_temp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_error = $files['error'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
if(in_array($file_ext, $allowed)){
if($file_error === 0){
if($file_size <= 20000000){
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = '../files/wcfiles/'.$file_name_new;
array_push($file_db_path_array, 'msg_files/'.strtolower($file_name_new));
if(move_uploaded_file($file_temp, $file_destination)){
$uploaded[$position] = $file_destination;
} else {
$failed[$position] = "[{$file_name}] failed to upload";
}
} else {
$failed[$position] = "[{$file_name}] is too large.";
}
} else {
$failed[$position] = "[{$file_name}] errored with code {$file_error}";
}
} else {
$failed[$position] = "[{$file_name}] file extension '{$file_ext}' is not allowed.";
}
}
$file_db_path = join(',', $file_db_path_array);
$ins_message = $conn->prepare("INSERT INTO conversation (msg_date, msg_id, msg_from, msg_to, msg_message, msg_files) VALUES (?,?,?,?,?,?)");
$ins_message->bind_param("ssssss", $msg_date, $msg_order_id, $msg_from, $msg_to, $msg_message, $file_db_path);
$ins_message->execute();
$ins_message->close();
稍后,当您想要显示每条消息的文件列表时,对于每条消息,使用逗号explode
文件列表来获取数组,然后遍历数组以显示每个文件。 / p>
// this is inside your messages display loop,
// assuming the files field is assigned to variable $files_string
echo '<ul>';
foreach ( explode(',', $files_string) as $file ) {
echo '<li><a href="../files/wcfiles/'.$file.'">'.$file.'</a></li>';
}
echo '</ul>';