dqwh1218 2016-05-13 02:16
浏览 211

file_put_contents的替代品

So if you checked my last post, I was uploading a 400MB file that ended up causing my VPS to lose all of its memory (2000MB memory to be exact) because it is uploading a file from one server to another server via cURL AND becasue I think file_put_contents is also the reason why it's such a resource hog.

So is there any alternatives to this code to save my memory usage?

$file = base64_decode($_POST['file']);
file_put_contents($_POST['filename'], $file);
  • 写回答

1条回答 默认 最新

  • dscrn1974 2017-02-16 15:07
    关注

    You can use cUrl: cURL can be used to grab data, information, or even a whole webpage from a designated URL. This can be very useful for grabbing information between sites. Example code:

    $url = "http://yourwebsite.com/path/imgtoread.jpg";
    $filetosave = PATH_ON_SERVER . "filetosave.jpg";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $fileraw = curl_exec($ch);
    curl_close ($ch);
    if(file_exists($filetosave)){
        unlink($filetosave);
    }
    $fp = fopen($filetosave,'x');
    fwrite($fp, $fileraw);
    fclose($fp);
    

    @var url url your website @filetosave path on your server where save file @fileraw content of file to save

    Note: and ensure that in php.ini allow_url_fopen is enable

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目