簡體   English   中英

文件上傳到服務器PHP

[英]File uploading to server PHP

首先,我想說我不是技術人員,我使用的腳本是我在網上找到的腳本。 有了我發現的腳本,我便開始研究它們,直到它完成了我想要的工作為止。

因此,我希望我的問題得到答復。

我已經制作了一份注冊表,希望用戶填寫他們的詳細信息,附加文件並將其發送到我的數據庫。

我已經對表格進行了調整,以便現在可以將所有內容很好地放入數據庫中。

但是,這是我的問題。 我希望將用戶附加的文件存儲在我的Web服務器上,以便在執行搜索時可以查看該文件。

在這里,我處理文件。 我希望有人願意幫我解決我做錯的事情。

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我知道這是一個很長的腳本,但是我還有很多其他事情,例如郵寄注冊確認郵件。 我試圖做一個將附件上傳到我的服務器文件夾“上載”的函數,但似乎沒有得到。

收集功能

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

}

>插入數據庫功能

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

因此,如果有人願意告訴我我需要怎么做才能獲得將附件上傳到我的文件夾“ uploads”中的功能,那將是非常有用的。

首先,將屬性enctype =“ multipart / form-data”添加到您的表單中

然后

代替

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

采用

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

用於獲取將存儲在數據庫中的文件名

還有其他文件數組可能對您有用

$_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

成功插入數據庫后

將文件復制到您的目錄(可以自定義)

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

您必須在enctype="multipart/form-data"添加屬性enctype="multipart/form-data" 當用戶提交表單時,文件將被上傳到您服務器上的tmp文件。 然后,您可以使用move_uploaded_file()將文件移動到所需位置。 希望這可以幫助

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