php 下载文件function.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 下载文件function.php相关的知识,希望对你有一定的参考价值。

<?php
/*
###################  READ ME  #################################
You'll pass the URL to your feed/file to this function inside the "Download from URL" option when creating an import. Examples:

1. [custom_file_download("ftp://username:password@hostname.com/full/path/to/file.csv", "csv")]
2. [custom_file_download("http://example.com/full/path/to/file.csv", "csv")]

You must add the code for the function inside your themes functions.php file, or in a plugin like Code Snippets.

This code is provided in the hope that it helps, but without any support.
###############################################################
*/

// Programmatically download and return import file via URL.
function custom_file_download($url, $type = 'xml'){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

    $headers = array();
    $headers[] = "Accept-Language: de";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
            exit('Error:' . curl_error($ch));
    }
    curl_close ($ch);

    $uploads = wp_upload_dir();
    $filename = $uploads['basedir'] . '/' . strtok(basename($url), "?") . '.' . $type;

    if (file_exists($filename)){
            @unlink($filename);
    }
    file_put_contents($filename, $result);
    return str_replace($uploads['basedir'], $uploads['baseurl'], $filename);

}

以上是关于php 下载文件function.php的主要内容,如果未能解决你的问题,请参考以下文章

php文件上传,提示移动文件失败

在'header.php'或'function.php'文件中插入Bootstrap目录更好吗?

PHP 图片库脚本:function.php文件

PHP 用于function.php文件的Wordpress侧边栏脚本

PHP 在Wordpress function.php文件中创建一个推特小部件

discuzX2/source/function/function_core.php通用核心函数库文件分析