简体   繁体   English

文件上传到服务器PHP

[英]File uploading to server PHP

First of all I want to say that I'm not a technical guy and that the scripts I use are scripts that I found online. 首先,我想说我不是技术人员,我使用的脚本是我在网上找到的脚本。 With the scripts that I found I than play around with them until it does what I want it to do. 有了我发现的脚本,我便开始研究它们,直到它完成了我想要的工作为止。

So I hope for kindness in the answers on my question. 因此,我希望我的问题得到答复。

I've made a registration form where I want the user to fil in their details, attache a file and sent it to my database. 我已经制作了一份注册表,希望用户填写他们的详细信息,附加文件并将其发送到我的数据库。

I've managed to tweak the forms so that at this moment I'm getting everything nicely into my database. 我已经对表格进行了调整,以便现在可以将所有内容很好地放入数据库中。

But, and this is my question. 但是,这是我的问题。 I want the file that the user attached stored on my webserver so when I perform a search I can view the file. 我希望将用户附加的文件存储在我的Web服务器上,以便在执行搜索时可以查看该文件。

Here I go with my files. 在这里,我处理文件。 I hope anyone is willing to help me out what I'm doing wrong. 我希望有人愿意帮我解决我做错的事情。

Index.php Index.php

<?PHP
require_once("./include/membersite_config.php");

if(isset($_POST['submitted']))
{
   if($fgmembersite->RegisterUser())
   {
        $fgmembersite->RedirectToURL("thank-you.html");
   }
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="HandheldFriendly" content="True"><meta name="MobileOptimized" content="320"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" templatewidth="940">
<link href="//fonts.googleapis.com/css?family=Arimo%3A100%2C100italic%2C200%2C200italic%2C300%2C300italic%2C500%2C500italic%2C600%2C600italic%2C700%2C700italic%2C800%2C800italic%2C900%2C900italic%2Citalic%2Cregular&amp;subset=all" rel="stylesheet" type="text/css">

<link rel="stylesheet" href="//ajrecruit.be/onewebstatic/e252b917fa.css">
<script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
<link rel="STYLESHEET" type="text/css" href="style/pwdwidget.css" />
<script src="scripts/pwdwidget.js" type="text/javascript"></script>
</head>
<body>
<div class="row" style="min-height:586px;width:940px;margin:auto;">
<div class="extra" style="margin:86px 320px 0px 220px;">
<div class="component  block idDD64D2318FF04CF59133D806E6A0D267" style="width:400px;">
<div class="self mobile-leaf contactFormContainer" style="width:400px;min-height:500px;" onclick="if(event.target ===  this) {event.stopPropagation();}">
<div id='fg_membersite'>



<form id='register' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>

<input type='hidden' name='submitted' id='submitted' value='1'/>

<div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div>

    <div class='container'>
    <label for='voornaam' >Voornaam*: </label><br/>
    <input type='text' name='voornaam' />
    </div>
    <div class='container'>
    <label for='familienaam' >Familienaam*: </label><br/>
    <input type='text' name='familienaam' />
    </div>
    <div class='container'>
    <label for='adres' >Adres*: </label><br/>
    <input type='text' name='adres' />
    </div>
    <div class='container'>
    <label for='postcode' >Postcode*: </label><br/>
    <input type='text' name='postcode' />
    </div>
    <div class='container'>
    <label for='gemeente' >Gemeente*: </label><br/>
    <input type='text' name='gemeente' />
    </div>
    <div class='container'>
    <label for='email' >Email*: </label><br/>
    <input type='text' name='email' />
    </div>
    <div class='container'>
    <label for='tele' >Telefoon*: </label><br/>
    <input type='text' name='tele' />
    </div>
    <div class='container'>
    <label for='username' >Username*: </label><br/>
    <input type='text' name='username' />
    </div>
    <div class='container'>
    <label for='password' >Wachtwoord*: </label><br/>
    <div class='pwdwidgetdiv' id='thepwddiv' ></div>
    <noscript>
    <input type='password' name='password' />
    </noscript>
    </div>
    <input type="file" name="file" />
    <button type="submit" name="btn-upload">upload</button>
    </form>
    <br /><br />

<script type='text/javascript'>
// <![CDATA[
    var pwdwidget = new PasswordWidget('thepwddiv','password');
    pwdwidget.MakePWDWidget();
// ]]>
</script>

</div>
</body>
</html>

fg_membersite.php I know it's a very long script but I does a bunch of other things, like mailing registration confirmation. fg_membersite.php我知道这是一个很长的脚本,但是我还有很多其他事情,例如邮寄注册确认邮件。 I've tried to make a function that uploads the attached file to my server folder "uploads", but don't seem to get it. 我试图做一个将附件上传到我的服务器文件夹“上载”的函数,但似乎没有得到。

collection function 收集功能

function CollectRegistrationSubmission(&$formvars)
    {
        $formvars['voornaam'] = $this->Sanitize($_POST['voornaam']);
        $formvars['familienaam'] = $this->Sanitize($_POST['familienaam']);
        $formvars['adres'] = $this->Sanitize($_POST['adres']);
        $formvars['postcode'] = $this->Sanitize($_POST['postcode']);
        $formvars['gemeente'] = $this->Sanitize($_POST['gemeente']);
        $formvars['email'] = $this->Sanitize($_POST['email']);
        $formvars['tele'] = $this->Sanitize($_POST['tele']);
        $formvars['file'] = $this->Sanitize($_POST['file']);
        $formvars['username'] = $this->Sanitize($_POST['username']);
        $formvars['password'] = $this->Sanitize($_POST['password']);

} }

> Insert into database function >插入数据库功能

function InsertIntoDB(&$formvars)
    {

        $confirmcode = $this->MakeConfirmationMd5($formvars['email']);

        $formvars['confirmcode'] = $confirmcode;

        $insert_query = 'insert into '.$this->tablename.'(
                voornaam,
                familienaam,
                adres,
                postcode,
                gemeente,
                email,
                tele,
                file,
                username,
                password,
                confirmcode
                )
                values
                (
                "' . $this->SanitizeForSQL($formvars['voornaam']) . '",
                "' . $this->SanitizeForSQL($formvars['familienaam']) . '",
                "' . $this->SanitizeForSQL($formvars['adres']) . '",
                "' . $this->SanitizeForSQL($formvars['postcode']) . '",
                "' . $this->SanitizeForSQL($formvars['gemeente']) . '",
                "' . $this->SanitizeForSQL($formvars['email']) . '",
                "' . $this->SanitizeForSQL($formvars['tele']) . '",
                "' . $this->SanitizeForSQL($formvars['file']) . '",
                "' . $this->SanitizeForSQL($formvars['username']) . '",
                "' . md5($formvars['password']) . '",
                "' . $confirmcode . '"
                )';      
        if(!mysql_query( $insert_query ,$this->connection))
        {
            $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
            return false;
        }        
        return true;
    }

So if anybody is willing to tell me how I need to do it to get a function that uploads the attached file into my folder 'uploads', that would be much appriciated. 因此,如果有人愿意告诉我我需要怎么做才能获得将附件上传到我的文件夹“ uploads”中的功能,那将是非常有用的。

first of all add attribute enctype="multipart/form-data" to your form 首先,将属性enctype =“ multipart / form-data”添加到您的表单中

then 然后

instead of 代替

$formvars['file'] = $this->Sanitize($_POST['file']);

use 采用

$formvars['file'] = $this->Sanitize($_FILES['file']['name']);

for getting file name which will be stored in database 用于获取将存储在数据库中的文件名

There are other files array which may be useful to you 还有其他文件数组可能对您有用

$_FILES['file']['name']; //contain name of file
$_FILES['file']['type'];//contain type (mime type) of file
$_FILES['file']['tmp_name'];//contain temporary path of file
$_FILES['file']['size'];//contain sizeof file

after successfull inserting in database 成功插入数据库后

copy file to your directory (may be custom) 将文件复制到您的目录(可以自定义)

move_uploaded_file($_FILES['file']['tmp_name'],"MY_PATH/".$_FILES['file']['name']);

you have to add the attribute enctype="multipart/form-data" to your form. 您必须在enctype="multipart/form-data"添加属性enctype="multipart/form-data" when the user submits the form the file will be uploaded to a tmp file on your server. 当用户提交表单时,文件将被上传到您服务器上的tmp文件。 you can then use move_uploaded_file() to move the file wherever you want. 然后,您可以使用move_uploaded_file()将文件移动到所需位置。 hope this helps 希望这可以帮助

ps docs: http://php.net/manual/en/features.file-upload.post-method.php ps docs: http : //php.net/manual/en/features.file-upload.post-method.php

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

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