无法使用Post上传表单中的文件

时间:2011-07-16 13:35:07

标签: php forms file post isset

我在将文件上传添加到现有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']);
}
}

//attach file开始,我试图让上传工作 - 感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

  

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

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

之前

答案 1 :(得分:0)

HTTP_X_REQUESTED_WITH

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

答案 2 :(得分: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