Я хотел бы указать, прежде чем я получу в этом, что я PHP newb, и я некоторое время боролся с этим, прежде чем окончательно решить, что я не знаю, что я Я делаю с этим. Я не думаю, что я что-то ошибся или неправильно капитализировал, как this guy, но, пожалуйста, простите мой грязный код.PHP: Имя файла не может быть пустым в file_get_contents
<?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 = '[email protected]';
$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 [email protected]');
window.location = 'contact.html';
</script>
<?php
}
?>
И я получаю эту ошибку:
Внимание: file_get_contents() [function.file-получить содержание]: Имя файла не может быть пустым в /%ROOT%/contact.php на линии 57
Мои вопросы: что я делаю неправильно, пытаясь передать загруженный файл в виде вложения? И почему файл $ path_of_uploaded_file кажется пустым? Кроме того, не все будут загружать файлы, так как я могу разрешить отправку?
вы, кажется, не определили '$ upload_folder' в коде, при условии, прежде чем использовать его в этой строке:' $ path_of_uploaded_file = $ upload_folder. $ name_of_uploaded_file; ' –
. Вы должны прочитать этот документ о загрузке и перемещении/подтверждении загрузки файлов: http://www.php.net/manual/en/features.file-upload.post-method.php –