Php 在数据库列中插入多个文件名
我有这个文件上传代码,这是工作良好。我唯一的问题是当我上传多个文件时,它会在数据库中为每个文件创建多行。例如,如果我上传5个文件,它将创建5个新行,其中包含相同的消息,但文件名不同。它不会将所有文件绑定到一条消息中,也不会为数据库中包含多个文件的消息创建一行Php 在数据库列中插入多个文件名,php,file-upload,Php,File Upload,我有这个文件上传代码,这是工作良好。我唯一的问题是当我上传多个文件时,它会在数据库中为每个文件创建多行。例如,如果我上传5个文件,它将创建5个新行,其中包含相同的消息,但文件名不同。它不会将所有文件绑定到一条消息中,也不会为数据库中包含多个文件的消息创建一行 if(!empty($_FILES['files']['name'][0])){ $files = $_FILES['files']; $uploaded = array(); $failed = array(
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();
if(!empty($_FILES['FILES']['name'][0])){
$files=$_文件['files'];
$upload=array();
$failed=array();
$allowed=array('jpg'、'jpeg'、'gif'、'png'、'txt'、'docx'、'docx'、'xls'、'xlsx'、'zip'、'rar'、'gz'、'7zip'、'ppt'、'pptx'、'rtf'、'pdf'、'svg'、'sav'、'csv');
foreach($files['name']作为$position=>$file\u name){
$file_temp=$files['tmp_name'][$position];
$file_size=$files['size'][$position];
$file_error=$files['error'][$position];
$file\u ext=explode('.',$file\u name);
$file_ext=strtolower(end($file_ext));
if(在数组中($file\u ext,$allowed)){
如果($file_error==0){
如果($file_size prepare(“插入对话(消息日期、消息id、消息发件人、消息收件人、消息文件))值(?,,,,,?,?)”;
$ins_message->bind_param(“ssssss”、$msg_date、$msg_id、$msg_from、$msg_to、$msg_message、$file_db_path);
$ins_消息->执行();
$ins_消息->关闭();
文件代码
echo '<ul>';
foreach (explode(',', $msg_rows['msg_files']) as $file ) {
echo '<li><a href="../files/wcfiles/"'.$file.'>'.$file.'</a></li>';}
echo '</ul>';
echo'';
foreach(将(',',$msg_行['msg_文件'])分解为$file){
回音“”;}
回声“
”;
我认为最好的方法是将文件路径值推送到foreach
循环中的一个数组中。然后,在循环之后,将每个文件路径数组元素连接到一个字符串中,每个元素之间带有逗号,以将完整的文件路径字符串插入数据库
首先在foreach
循环之前实例化一个数组,以保存文件路径值
$file_db_path_array = [];
foreach ($files['name'] as $position => $file_name) {
// ...
Next在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\u db\u路径
赋值行替换为:
array_push($file_db_path_array, 'msg_files/'.strtolower($file_name_new));
最后在数据库插入代码中,将$file\u db\u path
更改为连接(',',$file\u db\u path\u数组)
下面是完整的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();
感谢该死的兄弟,我正在测试它。非常感谢你的详细回答。如果我做错了什么,请检查代码并帮助我。我现在还在$file\u db\u path上得到未定义的变量。@stackkid我的错。我忘了在foreach
循环之前添加$file\u db\u path\u string='
,以实例化你需要的文件路径字符串循环时ild上升。我认为数组方法更优雅,所以我更新了我的答案,以采用该方法。@stackkid我看到你更改了问题代码,并且你试图包括两种方法。它们是独占的,一种或另一种,而不是两种。请再次检查我的答案。我已经测试了基本内容,没有出现错误。如果我没有我得到了以下数组([0]=>new.jpg)数组([0]=>new.jpg[1]=>old.jpg)
和错误(!)注意:第98行C:\wamp\www\cl\conversation.php中的数组到字符串转换…它似乎也在文件中循环,而不是一次附加所有文件。此外,在db中没有插入-
$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();
// 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>';