1

私はフォームで作業しており、ファイルのアップロードを次のスクリプトに追加したいと考えています。

//form validation vars
$formok = true;
$errors = array();

//submission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = $_POST['message'];

//validate form data

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}
//validate message is greater than 20 characters
elseif(strlen($message) < 20){
    $formok = false;
    $errors[] = "Your message must be greater than 20 characters";
}

//send email if all is ok
if($formok){
    $headers = "From: info@example.com" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                  <p><strong>Name: </strong> {$name} </p>
                  <p><strong>Email Address: </strong> {$email} </p>
                  <p><strong>Telephone: </strong> {$telephone} </p>
                  <p><strong>Enquiry: </strong> {$enquiry} </p>
                  <p><strong>Message: </strong> {$message} </p>
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    mail("enquiries@example.com","New Enquiry",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'enquiry' => $enquiry,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);

//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
    //set session variables
    session_start();
    $_SESSION['cf_returndata'] = $returndata;

    //redirect back to form
    header('location: ' . $_SERVER['HTTP_REFERER']);
}

}

また、ファイルの種類とサイズを制限し、ファイルのコピー (画像) を電子メールで送信して、/uploads にアップロードしたいと考えています。

使用する必要があると思いますが、使用$_FILES方法がわかりませんisset- 助けていただければ幸いです。HTML 要素の ID はuploaded_file.

ありがとう、

これを上記のコードに追加しましたが、まだ問題があります。

    $message = $_POST['message'];
$photo = $_FILES['uploaded_file']['tmp_name'];

//attach file
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploaded_file']['name']); 

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
    $formok = false;
    $errors[] = "You have not attached a photo";
}

明らかにこれは検証の問題ですが、理解できません...

4

2 に答える 2

0

HTMLで、

enctype="multipart/form-data"<form>タグおよび <input type="File" name="uploaded_file />アップロードボタンを配置するフォーム入力リストの属性として。

次に、上記のPHPスクリプトに次を追加します。

if($_FILES['uploaded_file']['tmp_name']){
   $file = file_get_contents($_FILES['uploaded_file']['tmp_name']); 
}
于 2011-07-15T21:16:18.390 に答える
0
* $_FILES["file"]["name"] - the name of the uploaded file
* $_FILES["file"]["type"] - the type of the uploaded file
* $_FILES["file"]["size"] - the size in bytes of the uploaded file
* $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
* $_FILES["file"]["error"] - the error code resulting from the file upload
  • [リンクは削除されました。コメント内のリンクを参照してください]

私はこれがあなたを助けると信じています. わかりやすくするために編集:ファイルをアップロードしたファイル フィールド"file"の属性です。name

アップロードしたら、次に何をすべきかについて、ここ (PHP.net)の例を確認してください。

于 2011-07-15T21:14:42.977 に答える