0

我正在尝试将图片上传并存储到我的数据库中。问题是,我对 HTML 不是很好,我只是复制/粘贴标签来获取我喜欢的表单并编辑我认为应该去的地方。我很确定它与我正在使用的 html 标签有关,因为它本身可以正常工作,但是当我将该代码复制/粘贴到我认为应该在我的注册表单上的位置时,if 语句检查类型和大小规定总是返回 false。它必须与粘贴到工作注册表单页面的表单/发布/提交按钮组合有关。

这是我的工作上传测试代码

<html>
<body>
<form   method  = "post" enctype = "multipart/form-data">
<label  for = "file">Filename:</label>
<input  type    = "file"   name = "file"   id = "file" />
<input  type    = "submit" name = "Submit" value ="Submit">
</form>

<?php
if (!empty($_FILES['file']))
{
    if ((( $_FILES["file"]["type"] == "image/gif")  
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
        && ($_FILES["file"]["size"] < 5000000))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            header("Location: http://www.drink-social.com/error.php?upload=".$_FILES["file"]["error"]);
            exit();
        }

        else if (file_exists("/var/www/pics/".$_FILES["file"]["name"]))
        {
            header("Location: http://www.drink-social.com/error.php?upload=exists");
            exit();
        }

        move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/pics/".$_FILES["file"]["name"]);
    }
}
?>
</body>
</html>

现在,这就是我的注册页面,在我将这些部分复制/粘贴在一起之后,看起来像:

<html>
<body>
Please enter your details below:
<br />
<form   name = "Details" method = "post">
<br />
Username:   <input type = "text" name = "Username">
<br />
Password:   <input type = "text" name = "Password">
<br />  
Name:       <input type = "text" name = "Name">
<br />
Address:    <input type = "text" name = "Line1">
<br />
Line 2:     <input type = "text" name = "Line2">
<br />
City:       <input type = "text" name = "City">
<br />
State:      <input type = "text" name = "State">
<br />
Zip:        <input type = "text" name = "Zip">
<br />
Now let's get those daily deals...
<br />
Monday:     <input type = "text" name = "Monday">
<br />
Tuesday:    <input type = "text" name = "Tuesday">
<br />
Wednesday:  <input type = "text" name = "Wednesday">
<br />
Thursday:   <input type = "text" name = "Thursday">
<br />
Friday:     <input type = "text" name = "Friday">
<br />
Saturday:   <input type = "text" name = "Saturday">
<br />
Sunday:     <input type = "text" name = "Sunday">
<br />

<form   method  = "post" enctype = "multipart/form-data">
<label  for = "file">Filename:</label>
<input  type    = "file"   name = "file"   id = "file" />
<input  type    = "submit" name = "Submit" value ="Submit">
</form>

<?php
include_once("host_class.php");
include_once("event_class.php");

if (!empty($_POST['Username']))
{           
    $host  = new Host();        
    $event = new Event();

    if ($host->i_exist($_POST['Username']))
    {
        header("Location: http://www.drink-social.com/error.php?login=duplicate");
        exit();
    }

    else if ((( $_FILES["file"]["type"] == "image/gif")     
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
        && ($_FILES["file"]["size"] < 5000000))
    {
        if ($_FILES["file"]["error"] > 0)
            {
                header("Location: http://www.drink-social.com/error.php?upload=".$_FILES["file"]["error"]);
            exit();
            }

        else if (file_exists("/var/www/pics/".$_FILES["file"]["name"]))
        {
            header("Location: http://www.drink-social.com/error.php?upload=exists");
            exit();
        }

        move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/pics/".$_FILES["file"]["name"]);
        $host->set_username($_POST['Username']);
        $host->set_password($_POST['Password']);
        $host->set_name($_POST['Name']);        
        $host->set_address($_POST['Line1'], $_POST['Line2'], $_POST['City'],
                   $_POST['State'], $_POST['Zip']);
        $host->set_pic($_FILES['file']['name']);

        $id = new MongoID($host->add_me());
        $host->build_me($id);

        $host->set_mondayID($event->add_me($_POST['Monday'], $host->get_id()));
        $host->set_tuesdayID($event->add_me($_POST['Tuesday'], $host->get_id()));
        $host->set_wednesdayID($event->add_me($_POST['Wednesday'], $host->get_id()));
        $host->set_thursdayID($event->add_me($_POST['Thursday'], $host->get_id()));
        $host->set_fridayID($event->add_me($_POST['Friday'], $host->get_id()));
        $host->set_saturdayID($event->add_me($_POST['Saturday'], $host->get_id()));
        $host->set_sundayID($event->add_me($_POST['Sunday'], $host->get_id())); 

        $host->update_me();
        header("Location: http://www.drink-social.com/member.php?ID=".$id);
        exit();
    }

    else 
    {
        header("Location: http://www.drink-social.com/error?upload=empty");
        exit();
    }

}
?>
</body> 
</html>

它通过检查帖子是否为空,但是当它查看是否有文件时,它返回 false 并将我带到我的错误页面并说没有选择文件......?我一无所知,请指教提前谢谢,抱歉这么久!

4

1 回答 1

1

对于上传文件,我正在使用此代码

假设我的输入字段为

<input type="file" value=""  name="file" />

现在点击提交我将收到文件为

if($_FILES['file']['name']){ 
    if($_FILES['file']['error']==0){
        $target = "/images/";
        $target_path = $target.time().getExtension($_FILES['file']['name']); 
        move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
    }
}

在这里,我正在使用当前时间戳保存文件作为名称并使用扩展名

function getExtension($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

现在我不确定 MongoDB,但从你的代码来看,它应该是

$host->set_pic($target_path); 

存储该图像的路径。检查文件类型和大小的其余条件可以放在此代码中的相应位置。谢谢

于 2012-05-03T10:56:46.150 回答