我正在尝试实现一个功能,大致如下:
- 如果文本区域有内容(非空)且未添加附件,则仅在div中显示消息。
- 如果文本区域为空,但已添加附件,则显示
下载
按钮,该按钮将强制消息接收者将附件保存到本地磁盘。 - 如果文本区域不为空且已添加附件,则同时显示消息和下载按钮。
我的当前情况:
目前,我有以下代码片段来处理附件(目前只能是图像)。理想情况下,我不想在服务器上存储任何东西:
首先,这里是我的消息页面的视觉表示以及我如何显示我的消息:
messages.php:
这是我的代码:
<?php
/**************************************************/
// 1. My textarea form for sending a message:
echo " <form action='messages.php?u=$user' method='post' enctype='multipart/form-data'>
<textarea name='msg_body' rows='3' maxlength='255' cols='110' placeholder='Send message...'></textarea>
<input type='submit' name='send' value='Send'/>
<input id='file-input' name='attachment' type='file'/>
</form>";
/*
When the above form is filled, the following states are considered valid:
1.1. If the textarea is not empty and no attachment is added.
1.2. If the textarea is empty but an attachment is added.
1.3. If both textarea and attachment are empty, then DO NOT execute the INSERT query.
*/
/**************************************************/
// 2. My approach to achieve the above and more...
if ($user != $username) {
if (isset($_POST['send'])) {
$msg_body = (trim(strip_tags(@$_POST['msg_body'])));
$date_of_msg = date("Y-m-d");
$read = "no";
$deleted = "no";
// check if file is added and message is placed
if (($_FILES['attachment']['size']) == 0 && ($_FILES['attachment']['error'] == 0) && $msg_body != "") {
// do nothing
} else {
if (isset($_FILES['attachment'])) {
// check format of file
if (((@$_FILES["attachment"]["type"] == "image/jpg")
|| (@$_FILES["attachment"]["type"] == "image/jpeg")
|| (@$_FILES["attachment"]["type"] == "image/png")
|| (@$_FILES["attachment"]["type"] == "image/gif"))
&& (@$_FILES["attachment"]["size"] < 3145728)) //3mb
{
if (file_exists("user_data/attached_files/".@$_FILES["attachment"]["name"])) {
// do nothing
} else {
// move temporary image files into one of the randomly generated files
move_uploaded_file(@$_FILES["attachment"]["tmp_name"], "user_data/attached_files/".@$_FILES["attachment"]
["name"]);
// get name
$attach_name = @$_FILES["attachment"]["name"];
}
}
}
$send_msg = mysqli_query($connect, "INSERT INTO private_messages VALUES ('','$username','$user', '$msg_body',
'$date_of_msg', '$read', '$deleted')
");
} // 396
echo "<meta http-equiv='refresh' content='0'>";
}
}
/**************************************************/
// 3. Now to display the download button (ONLY IF AN ATTACHMENT IS ADDED):
if ($msg_to == $user){
echo " <div class='parent'>
<div class='msg_prof'>
<img class='img-rounded' src='/user_data/profile_pics/$my_pro_pic'/>
</div>
<div class='new_msg_from_user'>
<p><b style= 'color: red;'> You said:</b> $msg_body</p>
<span class='faded'>$date </span>";
// check if file is empty
if (isset ($_FILES['attachment']['size']) == 0 && (isset($_FILES['attachment']['error'])) == 0){
// no file attached, so do nothing
} else {
echo " <form action='inc/download_attachment.php' method='post' enctype='multipart/form-data'>
<button type='submit' name='save'> Download</button>
</form>";
}
echo " <a href='inc/remove_message.php?id=$message_id'> Remove </a>
</div><hr/>
</div>";
}
?>
我的当前结果:
下面的图片展示了我的当前结果。最后一个空白的帖子(You said: ""
)是一个没有在textarea
中输入文本,但添加了附件的帖子。
![enter image description here](https://istack.dev59.com/HozMD.webp)
download_attachment.php
,强制用户将图像保存到本地磁盘。摘要:
- 如何仅在添加附件时显示下载按钮?
- 如何防止发送空消息?(即没有消息和附件)。