PHP文件上传设置

发布于 2024-11-24 09:39:33 字数 3583 浏览 1 评论 0原文

我正在处理一个表单,想要将文件上传添加到以下脚本:

//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: [email protected]" . "\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("[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']);
}

我还想限制文件

类型和大小,通过电子邮件发送文件副本(图像)并将其上传到 /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";
}

显然这是一个验证问题 - 但无法弄清楚......

I'm working on a form and want to add a file upload to the following script:

//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: [email protected]" . "\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("[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']);
}

}

I also want to restrict the file type and size, email a copy of the file (an image) and upload it to /uploads.

I think I need to use $_FILES but don't know how with isset - I'd really appreciate some help. The HTML element's id is uploaded_file.

Thanks,

I've added this to above code but am still having problems;

    $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";
}

Clearly this is a validation issue - but can't figure it out...

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

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

发布评论

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

评论(2

无妨# 2024-12-01 09:39:33
* $_FILES["file"]["name"] - 上传文件的名称
* $_FILES["file"]["type"] - 上传文件的类型
* $_FILES["file"]["size"] - 上传文件的大小(以字节为单位)
* $_FILES["file"]["tmp_name"] - 存储在服务器上的文件临时副本的名称
* $_FILES["file"]["error"] - 文件上传产生的错误代码
  • [链接已删除,请参阅评论中的链接]

我相信这会对您有所帮助。为了清楚起见,进行编辑:“file” 是您上传文件的文件字段的 name 属性。

上传后,请查看示例此处 (PHP.net)< /a> 了解下一步要做什么。

* $_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
  • [link removed, see link in comments]

I believe this will help you. Edit for clarity: "file" is the name attribute of the file field where you uploaded the file.

Once it's uploaded, check out the examples here (PHP.net) for what to do next.

雨轻弹 2024-12-01 09:39:33

在 HTML 中,将

enctype="multipart/form-data" 添加为

标记的属性,并

然后在上面的 PHP 脚本中添加:

if($_FILES['uploaded_file']['tmp_name']){
   $file = file_get_contents($_FILES['uploaded_file']['tmp_name']); 
}

In HTML, add

enctype="multipart/form-data" as an attribute to your <form> tag and
<input type="File" name="uploaded_file /> to your form inputs list where you want the upload button to be.

Then in your PHP script above add:

if($_FILES['uploaded_file']['tmp_name']){
   $file = file_get_contents($_FILES['uploaded_file']['tmp_name']); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文