前言
我想说我没有使用PHP的经验。我只是简单地使用常识来编辑PHP文件。
问题:
我最近开始在一个新网站上工作,我从头开始100%编写这个。但我想添加一个图像提交论坛,所以我浏览了一下网络,并访问了多个不同的网站,我能够得到以下的PHP代码(我已经测试过,它的工作原理)[这个PHP代码在LevelBRSubmit.php
文件中:
<?php
/* Set e-mail recipient */
$myemail = "example@email.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your Username");
$email = check_input($_POST['email'] "Provide your Email");
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Username: $yourname
E-mail: $email
Level Description:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, 'New Level Submition!', $message);
/* Redirect visitor to the thank you page */
header('Location: http://projectskyforums.x10.mx/');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
<input type="button" value="Back To Forum" />
</body>
</html>
<?php
exit();
}
?>
此论坛(位于BaseHTMLPlate.html
文件中):
<div class="textBox">
<p>Required fields are <b>bold</b></p>
<form action="LevelBRSubmit.php" method="post">
<p><b>Your Username:</b> <input type="text" name="yourname" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
</div>
当我添加图像上传按钮时,它全部停止工作(我已经假定会发生):
<div class="textBox">
<p>Required fields are <b>bold</b></p>
<form action="LevelBRSubmit.php" method="POST" enctype="multipart/form-data">
<p><b>Your Username:</b> <input type="text" name="yourname" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
<b>Your Level Design:</b><input type="file" value="Upload Image" name="pic" accept="image/*"><br />
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
</div>
PHP:
<?php
/* Set e-mail recipient */
$myemail = "example@email.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your Username");
$email = check_input($_POST['email'] "Provide your Email");
$pic = $_FILES['pic']['name'];
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Username: $yourname
E-mail: $email
Level Description:
$comments
Level Design: $pic
End of message
";
/* Send the message using mail() function */
mail($myemail, 'New Level Submition!', $message);
/* Redirect visitor to the thank you page */
header('Location: http://projectskyforums.x10.mx/');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
<input type="button" value="Back To Forum" >
</body>
</html>
<?php
exit();
}
?>
未声明HTML文档的字符编码。如果文档包含US-ASCII范围之外的字符,则文档将在某些浏览器配置中使用乱码文本进行渲染。必须在文档或传输协议中声明页面的字符编码。
更新 PHP发送了电子邮件!但是,电子邮件不是显示图像,而是显示图像名称。
如果有人想让页面本身看一下你可以在下面的网站链接找到它(该网站仍处于开发的早期阶段): http://projectskyforums.x10.mx/BaseHTMLPlate.html
答案 0 :(得分:3)
您必须在表单标记中使用class AddAllowOwnerBeDestroyedColumnToCompanies < ActiveRecord::Migration
def change
add_column :companies, :allow_owner_be_destroyed, :boolean, default: false
end
end
class Company < ActiveRecord::Base
belongs_to :owner, class_name: 'User', foreign_key: 'owner_id'
has_many :employees, class_name: 'User', dependent: :destroy
# You won't need this method b/c you now have `#allow_owner_be_destroyed?` for free
# def can_owner_be_destroyed?
# p allow_owner_be_destroyed
# p object_id
# allow_owner_be_destroyed
# end
def destroy(*args, &block)
update allow_owner_be_destroyed: true
super
end
end
class User < ActiveRecord::Base
belongs_to :company
def destroy(*args, &block)
return unless can_be_destroyed?
super
end
def can_be_destroyed?
!is_owner? || company.allow_owner_be_destroyed?
end
end
并在php文件中接收文件,您必须使用enctype="multipart/form-data"
来获取文件数据。
$_FILES['pic']
将此行替换为
<form action="LevelBRSubmit.php" method="post">
在 <form action="LevelBRSubmit.php" method="post" enctype="multipart/form-data">
文件中更改此行
LevelBRSubmit.php
到这个
$pic = check_input($_POST['pic'] "Provide your Design");
新行为您提供图像文件的名称。您可以使用$pic = $_FILES['pic']['name'];
查找图像文件的所有数据。