首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将file_get_contents转换为$_FILES

将file_get_contents转换为$_FILES
EN

Stack Overflow用户
提问于 2018-09-20 15:45:54
回答 1查看 322关注 0票数 0

我有一个功能,将获得用户输入和表单中的文件输入,并将其作为电子邮件发送。我使用以下代码来捕获这些文件:

代码语言:javascript
复制
$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。但是如果我有一个固定的文件,比如:

代码语言:javascript
复制
https://www.myurl.com/A_Beginners_Guide_to_Outsourcing.pdf

如何将其传递到附件数组中?我正在尝试:

代码语言:javascript
复制
$attachments_array[] = file_get_contents('https://www.myurl.com/A_Beginners_Guide_to_Outsourcing.pdf');

但似乎不起作用。我想像$data变量一样在附件数组中传递它。

EN

回答 1

Stack Overflow用户

发布于 2018-09-20 16:08:19

file_get_contents只获取文件的实际内容并返回它。

您需要创建一个关联数组并自己填充所有使用的参数,并可能将文件保存在本地某个地方,这取决于文件处理过程中的进一步步骤。

代码语言:javascript
复制
$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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52420089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档