文件上传不起作用表单标签 PHP MongoDB


File Upload Not Working Form Tag PHP MongoDB

我正在尝试上传图片并将其存储到我的数据库中。 问题是,我对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 并将我带到我的错误页面并说没有选择文件......?我一无所知,请指教提前感谢,对不起这么久!

为了上传文件,我正在使用此代码

假设我在表单中有输入字段

<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); 

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

相关文章: