2017-03-12 4 views
1

私はPHPフォームを作成しています。私はユーザーがmp3ファイル、pdfs、pngsまたはjpgsのいずれかをアップロードすることを許可したいと思います。フォームの他のすべての側面は機能するようです。ただし、添付ファイルは0 kbとなります。私のphp.iniでは、私は大きなファイルサイズの制限があります。以下は、私が使用していたコードです:特定のファイルタイプをPHPメールフォームにアップロードすることを許可する

<?php 
    if(isset($_POST['submit'])) 
    { 
    //The form has been submitted, prep a nice thank you message 
    $output = '<h3>Thank you for your submission!</h3>'; 
    //Set the form flag to no display 
    $flags = 'style="display:none;"'; 

    //Email 
    $to = '[email protected]'; 
    $subject=" $servicetype Music Scribes Inquiry"; 

    $name=$_REQUEST['name']; 
    $email=$_REQUEST['email']; 
    $servicetype=$_REQUEST['servicetype']; 
    $message = strip_tags($_POST['message']); 
    $attachment = $_FILES['file']['tmp_name']; 
    $file = $_FILES['file']['name']; 

    $boundary =md5(date('r', time())); 

    $headers = "From: $name<$email>\r\nReturn-path: $email"; 
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; 
    $message="This is a multi-part message in MIME format. 

    --_1_$boundary 
    Content-Type: multipart/alternative; boundary=\"_2_$boundary\" 

    --_2_$boundary 
    Content-Type: text/plain; charset=\"iso-8859-1\" 
    Content-Transfer-Encoding: 7bit 

    Service Type: $servicetype 
    Message: $message 

    --_2_$boundary-- 
    --_1_$boundary 
    Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3, application/pdf, image/jpeg, image/png; name=\"$file\" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 

    $attachment 
    --_1_$boundary--"; 

    mail($to, $subject, $message, $headers); 
    } 
    ?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>MailFile</title> 
    </head> 

    <body> 

    <?php echo $output; ?> 

    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
    <font size=2> 

    <p><label for="name">Name<font color=red>*</font></label><br><input type="name" name="name" required><br><br> 
    <p><label for="email">Email<font color=red>*</font></label><br><input type="email" name="email" required><br><br> 

    Transcription Service Type: 
    <select name="servicetype"> 
    <option value="Transcription">Transcription</option> 
    <option value="Arrangement">Arrangement</option> 
    <option value="Scoring">Scoring</option> 
    <option value="Other">Other</option> 
    </select><br><br> 

    <p><label for="message">Message<font color=red>*</font></label><br> 
    <textarea name="message" id="message" cols="40" rows="8" required> 
    </textarea></p><br> 

    <p><label for="file">Upload File</label> <input type="file" name="file" id="file"></p> 
    <p><input type="submit" name="submit" id="submit" value="send"></p> 
    </form> 
    </body> 
    </html> 

私はフォームを通じてmp3のアップロード、PDFや画像のためにすべての作業を変更する必要があります知っている聞かせください。ありがとう!

答えて

0

ファイルは正しくアップロードされていますが、電子メールメッセージに追加すると機能しません。

サードパーティのライブラリを使用して作業を簡単にすることをおすすめします。単純な電子メールメッセージを送信する場合、PHPのメール機能はうまく動作しますが、電子メールに添付ファイルを送信したり、HTMLを使用したりする場合は、やや複雑になります。もちろんそれは可能ですが、PHPMailerのようなライブラリを使うと作業が楽になります。

Githubのページには、使用方法の例があります。添付ファイルを追加するには、$email->AddAttachment('path/to/file.type', 'fileName.type');

0

あなたはどこにファイルをアップロードしていますか?ファイルのアップロード処理が表示されません。 この例を見てください:Handling file uploads(特にmove_uploaded_file部分)。

なぜあなたのコードにREQUESTとPOSTのスーパーグローバルを混在させていますか? $_REQUEST['email']ですが、$_POST['message']です。すべてのデータが請求後のものである場合は、$_REQUESTをすべて$_POSTに変更してください。

関連する問題