0

我在将文件上传添加到现有 POST 脚本时遇到问题:

if( isset($_POST)){

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

//sumbission 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'];
$photo = $_FILES['uploaded_file']['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)) {
echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

if($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK){
    $formok = false;
    $errors[] = "You have not attached a photo";
}

//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 charcters
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: contact@hostelcities.com" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<p>You have recieved 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("contact@hostelcities.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']);
}
}

From//attach file是我试图使上传工作的地方 - 我会很感激一些帮助谢谢。

4

3 回答 3

1

if($_FILES['uploaded_file']['error'] === UPLOAD_ERR_OK){

表示没有错误,它是唯一的“无错误”值,您将其视为错误。应该是 != 代替。并且此检查必须在 move_uploaded_file() 之前

于 2011-07-16T13:56:24.157 回答
0
HTTP_X_REQUESTED_WITH

这是否意味着您正在尝试通过 XHR 上传文件?你不能。

于 2011-07-16T13:54:00.907 回答
0

补充其他答案:

1) 在您的表单 HTML 中,确保包含enctype.

<form method="POST" action="uploader.php" enctype="multipart/form-data">
    <input type="FILE" name="uploaded_file"/>
</form>

2) 确保uploads/具有正确的文件权限以允许 r/w

于 2011-07-16T14:29:35.753 回答