php 无法打开流:HTTP 包装器不支持可写连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9748076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:34:07  来源:igfitidea点击:

failed to open stream: HTTP wrapper does not support writeable connections

phpcaching

提问by Django Anonymous

I have uploaded my localhost files to my website but it is showing me this error:-

我已将本地主机文件上传到我的网站,但它向我显示此错误:-

: [2] file_put_contents( ***WebsiteURL*** /cache/lang/ ***FileName*** .php) 
[function.file-put-contents]: failed to open stream: HTTP wrapper does 
not support writeable connections | LINE: 127 | FILE: /home/content/
***Folders\FileName*** .php

What i personally feel that the contents get saved in a file in cache folder and when i uploaded the files to my web server it is trying to access the cached localhost folder.

我个人认为内容保存在缓存文件夹中的文件中,当我将文件上传到我的 Web 服务器时,它试图访问缓存的 localhost 文件夹。

回答by drew010

Instead of doing file_put_contents(***WebSiteURL***...)you need to use the server path to /cache/lang/file.php(e.g. /home/content/site/folders/filename.php).

而不是这样做,file_put_contents(***WebSiteURL***...)您需要使用服务器路径/cache/lang/file.php(例如/home/content/site/folders/filename.php)。

You cannot open a file over HTTPand expect it to be written. Instead you need to open it using the local path.

你不能打开一个文件HTTP并期望它被写入。相反,您需要使用本地路径打开它。

回答by The Bumpaster

you could use fopen() function.

你可以使用 fopen() 函数。

some example:

一些例子:

$url = 'http://doman.com/path/to/file.mp4';
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/downloads/';


    $newfname = $destination_folder .'myfile.mp4'; //set your file ext

    $file = fopen ($url, "rb");

    if ($file) {
      $newf = fopen ($newfname, "a"); // to overwrite existing file

      if ($newf)
      while(!feof($file)) {
        fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );

      }
    }

    if ($file) {
      fclose($file);
    }

    if ($newf) {
      fclose($newf);
    }

回答by Birendra Maharjan

May this code help you. It works in my case.

愿此代码对您有所帮助。它适用于我的情况。

$filename = "D:\xampp\htdocs\wordpress/wp-content/uploads/json/2018-10-25.json";
    $fileUrl = "http://localhost/wordpress/wp-content/uploads/json/2018-10-25.json";
    if(!file_exists($filename)):
        $handle = fopen( $filename, 'a' ) or die( 'Cannot open file:  ' . $fileUrl ); //implicitly creates file
        fwrite( $handle, json_encode(array()));
        fclose( $handle );
    endif;
    $response = file_get_contents($filename);
    $tempArray = json_decode($response);
    if(!empty($tempArray)):
        $count = count($tempArray) + 1;
    else:
        $count = 1;
    endif;
    $tempArray[] = array_merge(array("sn." => $count), $data);
    $jsonData = json_encode($tempArray);
    file_put_contents($filename, $jsonData);