search
Homephp教程php手册一个获取远端文件的函数(Linux和Windows均适用)

window|函数

为了获取远端文件,编写了下面的一个小函数,基本各方面都考虑了,大家看看吧:
function getpic($url,$dir,$name){
    //获取url文件,然后存放到dir目录中,以name为文件名。
    //如果缺省文件名,则保留原文件名。
    $tmp=split("/",$url);
    $fname=$tmp[count($tmp)-1];//获取文件名
    if(empty($name)) {
        $name=$fname;//设定好文件名
    }else{//判断是否更改了文件的扩展名,如果更改的话,则改为文件本身的扩展名。
        $tmp1=split("\.",$fname);
        $tmp2=split("\.",$name);
        if(strcmp($tmp1[1],$tmp2[1])!=0) $name=$tmp2[0].".".$tmp1[1];
    }
    if(strstr($dir,"\\")){//说明是Windows环境下的目录
        if(substr($dir,-2)=="\\") $file=$dir.$name;else $file=$dir."\\".$name;
    }else{        
        if(substr($dir,"/")){//说明是Linux环境下的目录
            if(substr($dir,-1)=="/") $file=$dir.$name;else $file=$dir."/".$fname;
        }
        else{ //给了一个非法的目录,函数退出
            echo "error directory!";
            return ;
        }
    }
    if(!(file_exists("$dir") && is_dir("$dir"))) {//如果目录不存在,则创建目录
        mkdir($dir,0777);
    }
    if(file_exists($file)) return;//如果文件存在,则放弃操作
    $fpo=fopen($file,"wb");
    if(!$fpo){
        echo "Sorry,create file:$file failure!Check your right!";
        return ;
    }
    $num=0;
    do{
        $num++;
        $fpi=fopen($url,"r");
        if(!$fpi) echo "open remote file falure...Retry!\r\n";
    }while(!$fpi && $num    if(!fpi){
        echo "The network is buzy or Bad URL!";
        return ;
    }else echo "Open file OK!Now transfer....";
    while(!feof($fpi)){
        $cont=fread($fpi,128);
        fwrite($fpo,$cont,128);
    }
    fclose($fpi);
    fclose($fpo);
    echo "Success!";
}



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.