简体   繁体   English

HTML / php论坛图片上传有问题

[英]Having trouble with HTML/php forum image upload

Preface: 前言:

I would like to state that I have no experience with PHP. 我想声明我没有PHP经验。 I've been making edits to the PHP file by simply using common sense. 我一直只是通过使用常识来对PHP文件进行编辑。

Problem: 问题:

I've recently started working on a new website and I'm coding this one 100% from scratch. 我最近开始在一个新网站上工作,我从头开始对此代码进行100%的编码。 But I want to add a image submission forum and so I looked around the web for a little bit and from visiting multiple different websites I was able to come up with the following php code (which I have tested and it works)[This php code is in a LevelBRSubmit.php file]: 但是我想添加一个图像提交论坛,因此我在网上浏览了一下,通过访问多个不同的网站,我能够提出以下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();
}
?>

Along with this forum (which is in an BaseHTMLPlate.html file): 与此论坛一起(位于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>

When I added the image upload button it all stopped working (which I already assumed would happen): 当我添加图像上传按钮时,所有按钮都停止工作(我已经假设会发生):

<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:

<?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();
}
?>

The character encoding of the HTML document was not declared. 未声明HTML文档的字符编码。 The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. 如果文档包含来自US-ASCII范围之外的字符,则在某些浏览器配置中,文档将呈现乱码。 The character encoding of the page must be declared in the document or in the transfer protocol. 页面的字符编码必须在文档或传输协议中声明。

Update The PHP sent the email! 更新 PHP发送了电子邮件! But instead of displaying the image the email simply displays the images name. 但是,电子邮件不会显示图像,而只会显示图像名称。

If anyone would like to give the page itself a look you can find it at the website link below (the website is still in early stages of development): http://projectskyforums.x10.mx/BaseHTMLPlate.html 如果有人想看看页面本身,则可以在下面的网站链接中找到它(该网站仍处于开发的早期阶段): http : //projectskyforums.x10.mx/BaseHTMLPlate.html

You have to use enctype="multipart/form-data" in your form tag and to receive file in your php file you have to use $_FILES['pic'] to get your file data. 您必须在表单标签中使用enctype="multipart/form-data"并在php文件中接收文件,您必须使用$_FILES['pic']来获取文件数据。

<form action="LevelBRSubmit.php" method="post">

Replace above line with this 替换上面的行

<form action="LevelBRSubmit.php" method="post" enctype="multipart/form-data">

In your LevelBRSubmit.php file change this line 在您的LevelBRSubmit.php文件中,更改此行

$pic    = check_input($_POST['pic'] "Provide your Design");

to this 对此

$pic = $_FILES['pic']['name'];

The new line give you the name of image file. 新行为您提供图像文件的名称。 You can find all data using $_FILES['pic'] for your image file. 您可以使用$_FILES['pic']查找图像文件中的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM