我有一个功能,将获得用户输入和表单中的文件输入,并将其作为电子邮件发送。我使用以下代码来捕获这些文件:
$attachments_array = NULL;
foreach($_FILES as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if($tmp_name != ""){
$data=array(
'tmpfname' => $tmp_name,
'filename' => $name,
'type' => $type,
);
$attachments_array[] = $data;
}
}因此,我将获取用户文件输入并将其放入$attachments_array。但是如果我有一个固定的文件,比如:
https://www.myurl.com/A_Beginners_Guide_to_Outsourcing.pdf如何将其传递到附件数组中?我正在尝试:
$attachments_array[] = file_get_contents('https://www.myurl.com/A_Beginners_Guide_to_Outsourcing.pdf');但似乎不起作用。我想像$data变量一样在附件数组中传递它。
发布于 2018-09-20 16:08:19
file_get_contents只获取文件的实际内容并返回它。
您需要创建一个关联数组并自己填充所有使用的参数,并可能将文件保存在本地某个地方,这取决于文件处理过程中的进一步步骤。
$row = array();
$row['filename'] = 'A_Beginners_Guide_to_Outsourcing.pdf';
$row['type'] = "application/pdf"; // you might need some other method of getting a file type.
$tmp_path = "/my/tmp/path/".$row['filename'];
$body = file_get_contents('https://www.myurl.com/A_Beginners_Guide_to_Outsourcing.pdf');
file_put_contents($tmp_path, $body); // btw, be careful of what you save on your server..
$row['tmpfname'] = $tmp_path;
$attachments_array[] = $row;https://stackoverflow.com/questions/52420089
复制相似问题