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

问题描述 投票:69回答:3

我已将我的localhost文件上传到我的网站,但它显示我这个错误: -

: [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

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

php caching
3个回答
141
投票

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

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


12
投票

你可以使用fopen()函数。

一些例子:

$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);
    }

0
投票

愿这段代码可以帮到你。它适用于我的情况。

$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);
© www.soinside.com 2019 - 2024. All rights reserved.