PHP:file_get_contents 中的文件名不能为空

标签 php forms email server-side

我想在开始之前指出,我是一个 PHP 新手,在最终决定我不知道我在用它做什么之前,我已经为此苦苦挣扎了一段时间。我认为我没有拼写错误或大写错误,例如 this guy但是,请原谅我乱七八糟的代码。

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_comment = $_POST['comment'];
$field_question = $_POST['question'];
$field_support = $_POST['support'];
$field_steam = $_POST['steam'];
$field_file = $_POST['file'];
$field_message = $_POST['message'];
//Get the uploaded file information
$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
//get the file extension of the file
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs
//Settings
$max_allowed_file_size = 10000; // size in KB
$allowed_extensions = array("doc", "docx", "txt", "pdf", "rtf", "otf");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Size of file should be less than     $max_allowed_file_size";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}

$to = 'me@myemail.com';
$subject = 'Contact Form Message from '.$field_name;
$attachment = chunk_split(base64_encode(file_get_contents($path_of_uploaded_file)));

$boundary = md5(date('r', time())); 

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message Type: '." ";
$body_message .= ''.$field_comment." ";
$body_message .= ''.$field_question." ";
$body_message .= ''.$field_support." ";
$body_message .= ''.$field_steam."\n";
$body_message .= 'Message: '.$field_message;



$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$boundary."\""; 

$mail_status = mail($mail_to, $subject, $attachment, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to me@myemail.com');
window.location = 'contact.html';
</script>
<?php
}
?>

而且我一直收到这个错误:

警告:file_get_contents() [function.file-get-contents]:第 57 行/%ROOT%/contact.php 中的文件名不能为空

我的问题是,我尝试将上传的文件作为附件传递时做错了什么?而且,为什么 $path_of_uploaded_file 似乎是空的?另外,不是每个人都会上传文件,所以我怎么允许提交?

最佳答案

$_FILES['uploaded_file']['name'] 只是用户选择要上传的文件的名称
$_FILES['uploaded_file']['tmp_name'] 是上传到服务器的文件。
Php Post File Uploads

关于PHP:file_get_contents 中的文件名不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10661917/

相关文章:

html - 使用标签宽度排列表单元素时出现问题

css - 在 Firefox 中逐一排列输入元素

email - 使用成员的 "from"地址和 "sender" header 的潜在问题

java - 使用 pop3 在 Java 中获取草稿和发送项目

php - Laravel 文件上传添加到集合移动文件在数字文件夹中

ruby-on-rails - ActiveRecord 错误消息 : translation for fields

php - 更新后PHP执行超时

django - 验证电子邮件是从单元测试发送的

php - Laravel-5 添加 hasRole 方法到 Auth

php - Doctrine 上的复杂 SQL 查询