1

私のウェブサイトには、次のHTML5フォームがあります。

<form action="briefform.php" method="post" enctype="multipart/form-data" name="servicesform" id="servicesform" autocomplete="on"> 
    <fieldset>
        <ul>
            <li><label>Name*</label>
                <input name="name" type="text" class="name">
            </li>
            <li><label>Email*</label>
                <input name="email" type="email" class="email">
            </li>
            <li><label>Business Name</label>
                <input name="busname" type="text" id="busname">
            </li>
            <li><label>Business Description</label>
                <textarea name="busdisc" id="busdisc"></textarea>
            </li>
            <li><label>Budget (AUD)</label>
                <input name="budget" type="number" id="budget" placeholder="$">
            </li>
            <li><label>Time Frame</label>
                <input name="timeframe" type="text" id="timeframe">
            </li>
            <li><label>Project Title</label>
                <input name="protitle" type="text" id="protitle" >
            </li>
            <li><label>Project Description*</label>
                <textarea name="prodisc" id="prodisc" spellcheck="true"></textarea>
            </li>
            <li><label>Upload</label>
                <input name="uploads[]" type="file" id="uploads" multiple>
            </li>
            <li><label>Target Audience</label>
                <textarea name="target" id="target"></textarea>
            </li>
            <li><label>Further Details</label>
                <textarea name="requirements" id="requirements" spellcheck="true"></textarea>
            </li>
            <li><input type="reset" name="reset" class="reset" value="Reset"/>
                <input type="submit" name="submit" class="submit" value="Send"/>
            </li>
        </ul>
    </fieldset>
</form>

これは、このPHPスクリプトによって処理されています。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Site Name</title>
<meta http-equiv="refresh" content="15;URL=http://mysiteaddress.com/">
</head>

<style>
    body {
        background: #202024;
        font: .75em Arial, Helvetica, sans-serif;
        color: #FFF;
        text-align: center;
        margin-top: 25%;
    }
</style>
<body>

<?php
    if(isset($_POST['email'])) {

        // TO AND FROM
        $email_to = "myemail@address.com";
        $email_subject = "Message from MYSITE.COM";

        function died($error) {
            // ERROR MESSAGES TO THE USER
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        // VALIDATION ON EXPECTED DATA
        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['busname']) ||
            !isset($_POST['busdisc']) ||
            !isset($_POST['budget']) ||
            !isset($_POST['timeframe']) ||
            !isset($_POST['protitle']) ||
            !isset($_POST['prodisc']) ||
            !isset($_POST['target']) ||
            !isset($_POST['requirements'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }

        $name_from = $_POST['name']; // required
        $email_from = $_POST['email']; // required
        $busname = $_POST['busname']; // not required
        $busdisc = $_POST['busdisc']; // not required
        $budget = $_POST['budget']; // not required
        $timeframe = $_POST['timeframe']; // not required
        $protitle = $_POST['protitle']; // not required
        $prodisc = $_POST['prodisc']; // required
        $uploads = $_POST['uploads']; // not required
        $target = $_POST['target']; // not required
        $requirements = $_POST['requirements']; // not required         

        // MANDATORY FIELDS 
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
          if(!preg_match($email_exp,$email_from)) {
            $error_message .= 'The email address you entered does not appear to be valid.<br />';
          }
            $string_exp = "/^[A-Za-z .'-]+$/";
          if(!preg_match($string_exp,$name_from)) {
            $error_message .= 'The name you entered does not appear to be valid.<br />';
          }
          if(!preg_match($string_exp,$prodisc)) {
            $error_message .= 'The project description you entered does not appear to be valid.<br />';
          }

          if(strlen($error_message) > 0) {
            died($error_message);
          }
        $email_message = "Services Form.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name_from)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Business Name: ".clean_string($busname)."\n";
        $email_message .= "Business Description: ".clean_string($busdisc)."\n";
        $email_message .= "Budget: ".clean_string($budget)."\n";
        $email_message .= "Timeframe: ".clean_string($timeframe)."\n";
        $email_message .= "Project Title: ".clean_string($protitle)."\n";
        $email_message .= "Project Description: ".clean_string($prodisc)."\n";
        $email_message .= "Uploads: ".clean_string($uploads)."\n";
        $email_message .= "Target Audience: ".clean_string($target)."\n";
        $email_message .= "Further Requirements: ".clean_string($requirements)."\n";

        // FILE UPLOADS
        $allowedExts = array("ai", "doc", "docx", "gif", "jpeg", "jpg", "pdf", "png", "psd");
        $extension = end(explode(".", $_FILES["uploads"]["name"]));

        if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/png")
        || ($_FILES["file"]["type"] == "image/pjpeg"))
        && ($_FILES["file"]["size"] < 20000)
        && in_array($extension, $allowedExts)) {
           if ($_FILES["file"]["error"] > 0) {
             echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
             }
           else {
              echo "Upload: " . $_FILES["file"]["name"] . "<br />";
              echo "Type: " . $_FILES["file"]["type"] . "<br />";
              echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
              echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
                 if (file_exists("upload/" . $_FILES["file"]["name"])) {
                    echo $_FILES["file"]["name"] . " already exists. ";
           }
              else {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                 "upload/" . $_FILES["file"]["name"]);
                 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
              }
           }
        }
        else {
           echo "Invalid file";
        }

    // EMAIL HEADERS
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- SUCCESS MESSAGE -->
Your message has been sent.  Thank you for contacting me, I'll get back to you as soon as possible.

<?php
}
?>
</body>
</html>

ディレクトリにuploadというフォルダを作成しました。これは、スクリプトがファイルをアップロードする場所だと思います。

ユーザーが必須フィールドを含むフォームを送信すると、情報が私のメールアカウントに送信されます。

ただし、ユーザーが1つまたは複数のファイルも送信したい場合、スクリプトはこれを処理せず、ユーザーは「メッセージが送信されました。...」というフィードバック の上に「無効なファイル」エコーを受信します。

その後、自分の電子メールアカウントにアクセスすると、情報が正常に送信されたことがわかります(「無効なファイル」エラーが発生した場合でも)が、ユーザーがアップロードしたファイルが見つからず、アップロードフォルダーにありません。

上記のスクリプトを使用して、ファイルのアップロード部分をどのように実現しますか?

ありがとうございました。

4

1 に答える 1

2

OK..完全に新しい答え。コードにはいくつかの問題があります。サーバーで実行しています。

今は複数ファイルのアップロードに入るつもりはありません。

<form action="briefform.php" method="post" enctype="multipart/form-data" name="servicesform" id="servicesform" autocomplete="on"> 
    <fieldset>
        <ul>
            <li><label>Name*</label>
                <input name="name" type="text" class="name">
            </li>
            <li><label>Email*</label>
                <input name="email" type="email" class="email">
            </li>
            <li><label>Business Name</label>
                <input name="busname" type="text" id="busname">
            </li>
            <li><label>Business Description</label>
                <textarea name="busdisc" id="busdisc"></textarea>
            </li>
            <li><label>Budget (AUD)</label>
                <input name="budget" type="number" id="budget" placeholder="$">
            </li>
            <li><label>Time Frame</label>
                <input name="timeframe" type="text" id="timeframe">
            </li>
            <li><label>Project Title</label>
                <input name="protitle" type="text" id="protitle" >
            </li>
            <li><label>Project Description*</label>
                <textarea name="prodisc" id="prodisc" spellcheck="true"></textarea>
            </li>
            <li><label>Upload</label>
                <input name="uploads" type="file" id="uploads" multiple>
            </li>
            <li><label>Target Audience</label>
                <textarea name="target" id="target"></textarea>
            </li>
            <li><label>Further Details</label>
                <textarea name="requirements" id="requirements" spellcheck="true"></textarea>
            </li>
            <li><input type="reset" name="reset" class="reset" value="Reset"/>
                <input type="submit" name="submit" class="submit" value="Send"/>
            </li>
        </ul>
    </fieldset>
</form>

だから、まず、フォームをに変更します

<input name="uploads" type="file" id="uploads" multiple>

さて、スクリプトについて。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Site Name</title>
<meta http-equiv="refresh" content="15;URL=http://mysiteaddress.com/">
</head>

<style>
    body {
        background: #202024;
        font: .75em Arial, Helvetica, sans-serif;
        color: #FFF;
        text-align: center;
        margin-top: 25%;
    }
</style>
<body>

<?php
    if(isset($_POST['email'])) {

        // TO AND FROM
        $email_to = "myemail@address.com";
        $email_subject = "Message from MYSITE.COM";

        function died($error) {
            // ERROR MESSAGES TO THE USER
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        // VALIDATION ON EXPECTED DATA
        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['busname']) ||
            !isset($_POST['busdisc']) ||
            !isset($_POST['budget']) ||
            !isset($_POST['timeframe']) ||
            !isset($_POST['protitle']) ||
            !isset($_POST['prodisc']) ||
            !isset($_POST['target']) ||
            !isset($_POST['requirements'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');      
        }

        $name_from = $_POST['name']; // required
        $email_from = $_POST['email']; // required
        $busname = $_POST['busname']; // not required
        $busdisc = $_POST['busdisc']; // not required
        $budget = $_POST['budget']; // not required
        $timeframe = $_POST['timeframe']; // not required
        $protitle = $_POST['protitle']; // not required
        $prodisc = $_POST['prodisc']; // required
        $uploads = $_FILES['uploads']; // not required

$_POST['uploads']ではありません。$_FILESです。今後、スクリプトで$uploads値を使用する必要があります。

        $target = $_POST['target']; // not required
        $requirements = $_POST['requirements']; // not required         

        // MANDATORY FIELDS 
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
          if(!preg_match($email_exp,$email_from)) {
            $error_message .= 'The email address you entered does not appear to be valid.<br />';
          }
            $string_exp = "/^[A-Za-z .'-]+$/";
          if(!preg_match($string_exp,$name_from)) {
            $error_message .= 'The name you entered does not appear to be valid.<br />';
          }
          if(!preg_match($string_exp,$prodisc)) {
            $error_message .= 'The project description you entered does not appear to be valid.<br />';
          }

          if(strlen($error_message) > 0) {
            died($error_message);
          }
        $email_message = "Services Form.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name_from)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Business Name: ".clean_string($busname)."\n";
        $email_message .= "Business Description: ".clean_string($busdisc)."\n";
        $email_message .= "Budget: ".clean_string($budget)."\n";
        $email_message .= "Timeframe: ".clean_string($timeframe)."\n";
        $email_message .= "Project Title: ".clean_string($protitle)."\n";
        $email_message .= "Project Description: ".clean_string($prodisc)."\n";
        $email_message .= "Uploads: ".clean_string($uploads)."\n";
        $email_message .= "Target Audience: ".clean_string($target)."\n";
        $email_message .= "Further Requirements: ".clean_string($requirements)."\n";

        // FILE UPLOADS
        $allowedExts = array("ai", "doc", "docx", "gif", "jpeg", "jpg", "pdf", "png", "psd");
        $extension = end(explode(".", $_FILES["uploads"]["name"]));

ここまではすべて良いです。$ _FILES ["file"]を使い始めたところから、チュートリアルをつなぎ合わせたと思います。$_FILES["uploads"]または$uploadsのいずれかがこの時点で有効です

        if ((($_FILES["uploads"]["type"] == "image/gif")
        || ($_FILES["uploads"]["type"] == "image/jpeg")
        || ($_FILES["uploads"]["type"] == "image/png")
        || ($_FILES["uploads"]["type"] == "image/pjpeg"))
        && ($_FILES["uploads"]["size"] < 20000)

これは本当に小さな画像です。20000バイトはわずか20kBです。ここからこの部分を引き出したと思いますhttp://www.w3schools.com/php/php_file_upload.asp。あなたが使用したいかもしれません

       ($_FILES["uploads"]["size"] < 200000)   

200kBの画像の場合、または

       ($_FILES["uploads"]["size"] < 200000)   

2MBの画像の場合

        && in_array($extension, $allowedExts)) {

ここに別の問題があります。読みやすくするために、ifステートメントを展開します。

if(

    (
            ($_FILES["file"]["type"] == "image/gif"  ) ||   
            ($_FILES["file"]["type"] == "image/jpeg" ) || 
            ($_FILES["file"]["type"] == "image/png"  ) ||
            ($_FILES["file"]["type"] == "image/pjpeg")      

    )

    && 

            ($_FILES["file"]["size"] < 20000)

    && 

            in_array($extension, $allowedExts)

) 

基本的に、メタデータがgif、png、またはjpgの場合で、20kb未満であり、Adobe Illustrator、Wordドキュメント、画像、PDF、またはフォトショップファイルのいずれかである場合

それが画像の場合、それは真実です。AI、PDF、またはDOCがアップロードタイプチェックに合格することはありません。

           if ($_FILES["uploads"]["error"] > 0) {
             echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
             }
           else {
              echo "Upload: " . $_FILES["uploads"]["name"] . "<br />";
              echo "Type: " . $_FILES["uploads"]["type"] . "<br />";
              echo "Size: " . ($_FILES["uploads"]["size"] / 1024) . " Kb<br />";
              echo "Temp file: " . $_FILES["uploads"]["tmp_name"] . "<br />";
                 if (file_exists("upload/" . $_FILES["uploads"]["name"])) {
                    echo $_FILES["uploads"]["name"] . " already exists. ";
           }
              else {
                move_uploaded_file($_FILES["uploads"]["tmp_name"],
                 "upload/" . $_FILES["uploads"]["name"]);
                 echo "Stored in: " . "upload/" . $_FILES["uploads"]["name"];
              }
           }
        }
        else {
           echo "Invalid file";
        }

    // EMAIL HEADERS
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers); 
?>

<!-- SUCCESS MESSAGE -->
Your message has been sent.  Thank you for contacting me, I'll get back to you as soon as possible.

<?php
}
?>
</body>
</html>
于 2012-10-23T10:09:05.607 に答える