无法使用 Post 以表单形式上传文件

发布于 2024-11-24 02:53:29 字数 3567 浏览 2 评论 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: [email protected]" . "\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("[email protected]","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 是我尝试使上传工作的地方 - 我希望得到一些帮助,谢谢。

I'm having trouble adding a file upload to an existing POST script:

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: [email protected]" . "\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("[email protected]","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 is where i've tried to make the upload work - I'd appreciate some help thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§对你不离不弃 2024-12-01 02:53:29

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

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

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

means that there is no error, and it is the only value that is 'no error', and you are treathing it as an error. Is should be != instead. And this check have to be before move_uploaded_file()

冷了相思 2024-12-01 02:53:29
HTTP_X_REQUESTED_WITH

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

HTTP_X_REQUESTED_WITH

Does this mean you are trying to upload files via XHR? You can't.

明天过后 2024-12-01 02:53:29

要补充其他答案:

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

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

2) 确保 uploads/具有允许读/写的正确文件权限

To supplement the other answers:

1) In your form HTML, make sure to include the enctype.

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

2) Make sure uploads/ has the correct file permissions to allow r/w

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文