标签: file-put-contents

file_put_contents不适用于json laravel和php

我尝试了所有东西将项目推送到json文件但是当呼叫 file_put_contents("file.json",$data)不起作用和页面关闭时.

这是我的控制器代码.

public function addJson()
{
    $titulo = Input::get('title');
    $name = Input::file('image')->getClientOriginalName();

    $arr = json_decode(file_get_contents("js/data.json"), true);
    array_push($arr['galerias'], array('id' => count($arr['galerias'])+1, 'name' => $name, 'img' => 'galeria/'.$name));
    $arr2 = $arr['galerias'];
    $arr = json_encode($arr);
    echo $arr;
    file_put_contents('js/data.json', $arr);
    return view('main.prueba')->with('name',$arr2);
}
Run Code Online (Sandbox Code Playgroud)

这是json文件.

{
 "galerias": [
{
  "id": 1,
  "name": "Fiesta Privada",
  "img": "galeria/FP.JPG"
},
{
  "id": 2,
  "name": "Fiesta Privada",
  "img": "galeria/FP2.JPG"
},
{
  "id": 3,
  "name": "Fiesta Privada",
  "img": "galeria/FP3.JPG"
},
{
  "id": 4,
  "name": …
Run Code Online (Sandbox Code Playgroud)

php json laravel file-put-contents

7
推荐指数
1
解决办法
523
查看次数

为什么不能将信息写入/ tmp目录而不是/ var/www/html?

LAMP安装在我的本地电脑上,因为我知道该字符串xxxx可以/tmp/test用下面的PHP函数写入.

file_put_contents("/tmp/test","test1 test2") 
Run Code Online (Sandbox Code Playgroud)

cat ajax_get.php

<?php
    $str = "test1 test2";
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    $str = implode(" ",$_GET);
    file_put_contents("/tmp/test",$str);
    print($str);
?>
Run Code Online (Sandbox Code Playgroud)

为什么命令file_put_contents("/tmp/test",$str);ajax_get.php不能工作?

这是没有用的,以取代file_put_contents

$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);
Run Code Online (Sandbox Code Playgroud)

如果我更改下面的语句,可能是目录权限的问题 ajax_get.php

    file_put_contents("/tmp/test",$str);
Run Code Online (Sandbox Code Playgroud)

    file_put_contents("test",$str);
Run Code Online (Sandbox Code Playgroud)

并运行上一个进程,在其中ajax_get.php创建一个文件/var/www/html/test

cat /var/www/html/test
test1 test2
Run Code Online (Sandbox Code Playgroud)

显示/tmp目录的权限.

ls -al /tmp
total 76
drwxrwxrwt 16 root    root    12288 Dec 10 18:39 .
drwxr-xr-x 23 root    root     4096 Dec  1 08:03 ..
Run Code Online (Sandbox Code Playgroud)

.是当前目录/tmp,其权限是777(rwxrwxrwx),为什么不能通过/tmpPHP …

php file-put-contents

7
推荐指数
1
解决办法
930
查看次数

PHP:“无法打开流:权限被拒绝”

当我尝试通过某些功能访问任何目录或文件时,我在服务器上得到了一些有趣的结果。我已将所有文件和目录权限设置为 777 并将内容所有者更改为 Apache,但我仍然收到错误消息。代码:
move_uploaded_file($_FILES['file']['tmp_name'], '/var/www/html/fileContent_Site/userData/'.$_SESSION['username'].DIRECTORY_SEPARATOR.$_FILES['file']['name']);
或者 file_put_contents('userData/userData.txt', $result,FILE_APPEND); mkdir("userData/".$register['username']);

对于“move_uploaded_file()”我得到:

move_uploaded_file(/var/www/php/Site/userData/radi/110729.png):无法打开流:/var/www/php/Site/upload.php 中的权限被拒绝

move_uploaded_file():无法将 /var/www/php/Site/upload.php 中的“/tmp/phpUFvMcn”移动到“/var/www/php/Site/userData/radi/110729.png”

对于“file_put_content()”和“mkdir()”

file_put_contents(userData/userData.txt):无法打开流:/var/www/php/Site/register.php 中的权限被拒绝

mkdir(): /var/www/php/Site/register.php 中的权限被拒绝

php file-permissions mkdir directory-permissions file-put-contents

6
推荐指数
2
解决办法
2万
查看次数

file_put_contents():connect()失败:AWS服务器中的权限被拒绝

$username_ftp = "user";
$password_ftp = "password";
$file_name = "remote-transfer.txt";
$url = "example.biz/test/" . $file_name;
$hostname = "ftp://$username_ftp:$password_ftp@$url";
$content = file_get_contents('index.html');
$options = array('ftp' => array('overwrite' => true));
$stream = stream_context_create($options);
file_put_contents($hostname, $content, 0, $stream);
Run Code Online (Sandbox Code Playgroud)

我尝试将文件从我的远程服务器放到另一台远程服务器,同时从本地主机工作文件中解开此代码,并将文件从我的ocal pc传输到服务器。但是,当我尝试从我的AWS服务器中删除此代码时,它不传输任何文件,当我检查错误日志文件时,它给出了

PHP Warning:  file_put_contents(): connect() failed: Permission denied 
PHP Warning:  file_put_contents(ftp://...@example.biz/remote-transfer.txt): failed to open stream: operation failed in
Run Code Online (Sandbox Code Playgroud)

我的测试 文件夹权限为777, 现在该怎么办,是否有任何服务器配置错过了。

php ftp amazon-web-services file-put-contents

5
推荐指数
1
解决办法
1176
查看次数

写入文本文件不适用于单击的第一个项目

我有一个奇怪的问题,我不知道出了什么问题.我正在编写美国的互动地图.用户单击状态,单击正在文本文件中记录.然后在地图上显示总点击次数.它基本上是围绕完整数据库的快速解决方法.

代码有效.每次单击状态时,都会将其添加到文本文件中.如果状态尚不存在,则为其创建条目.如果是,则只需更新点击次数.这是文件:

<?php 
    // get the input from AJAX
    @$state = $_GET['state'];
    // get the txt file where all of the states are 
    $file = 'state_count.txt';
        //if state_count.txt exists 
        if($fopen = fopen($file, 'c')){ 
            //open it and check if the name of the state is recorded or not 
            if($strpos= strpos(file_get_contents($file), $state)){
                //if so, add 1 to the value after the state's name
                // in the formate State:#
                //cut the text file into an array by lines 
                $lines = file($file);
                //foreach line, …
Run Code Online (Sandbox Code Playgroud)

php text fopen file-get-contents file-put-contents

5
推荐指数
1
解决办法
50
查看次数

为什么file_put_contents()有时会抛出"No error"错误?

为什么这段代码有时会发出No such file or directory警告,但有时会发出No error警告;

<?php

file_put_contents('*bad*', 'hello world');    // Trigger illegal filename.

?>
Run Code Online (Sandbox Code Playgroud)

通常会给出此错误警告;

Warning: file_put_contents(*bad*): failed to open stream: No such file
or directory in C:\root\noerror.php on line 3
Run Code Online (Sandbox Code Playgroud)

但是,如果我等一下再运行一次,有时会说出来;

Warning: file_put_contents(*bad*): failed to open stream: No error in
C:\root\noerror.php on line 3
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

我使用的是Windows 7,PHP 5.5.11和nginx 1.4.6.在我的php.ini中,我有;

error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
report_memleaks = On
track_errors = On
html_errors …
Run Code Online (Sandbox Code Playgroud)

php warnings nginx file-put-contents

5
推荐指数
1
解决办法
524
查看次数

Foreach循环仅返回多维数组PHP上的第一个索引

让我解释一下我的情况,我正在为我的公司制作邮件模板。该邮件模板必须完全使用HTML。邮件模板包含我们商店中的其他产品。该模板具有带有有关产品信息的框。让我向您展示我的模板:

mailing-template.html

         <div>
                    ##PRODUCT_NAME##                     <br />    
                    Onderdeel Nr: ##PRODUCT_ID##         <br />  
                    Merk: ##PRODUCT_BRAND##              <br />  
                    Prijs: €##PRODUCT_PRICE##            <br />
         </div>
Run Code Online (Sandbox Code Playgroud)

您可能会说,## PRODUCT_INFORMATION#是我的产品信息将到的地方。在PHP的帮助下,我可以替代它。我从不同的数组中获取产品信息,如下所示:

数组

$products = array
(
    array
    (
        'product_id' => 'RD55559955',
        'product_name' => 'Ontsteking (DI) Cassette Zwart Origineel Saab 9-3/9-5',
        'price_excl' => 22600,
        'product_brand' => 'Origineel Saab',
    ),

    array
    (
        'product_id' => 'RD55559955',
        'product_name' => 'een ander product',
        'price_excl' => 22600,
        'product_brand' => 'Origineel Saab',
    ),

    array
    (
        'product_id' => 'RD55559955',
        'product_name' => 'nog een ander product',
        'price_excl' => …
Run Code Online (Sandbox Code Playgroud)

html php arrays foreach file-put-contents

5
推荐指数
1
解决办法
74
查看次数

file_put_contents 返回正常,但没有创建文件

我正在尝试写入/tmp具有权限的目录中的文件:drwxrwxrwt。当我file_put_contentsphp -a交互式 shell 执行时,它工作正常并使用提供的内容创建一个文件。当我在代码中调用此函数时,它返回写入文件的字节数,但在目标目录中没有创建文件。php 和/或 apache2 日志中的任何位置都不会抛出错误。

代码中正在进行以下调用。

// both $k and $version are defined and have values
file_put_contents('/tmp/' . $k . '-version.php', '<?php $version = "' . $version . '";');
Run Code Online (Sandbox Code Playgroud)

我这里没有调试选项。这可能是什么原因造成的?

我通过命令行通过curl 调用该函数。

调试

@progman 在评论中提到的调试步骤:

变量转储

代码:

$path = '/tmp/' . $k . '-version.php';
file_put_contents($path, '<?php $version = "' . $version . '";');
var_dump($path, file_exists($path), is_file($path), is_writable($path));
Run Code Online (Sandbox Code Playgroud)

输出:

string(34) "/tmp/fullpagewidgetnew-version.php"
bool(true)
bool(true)
bool(true)
Run Code Online (Sandbox Code Playgroud)

Shell 命令执行

在代码中添加shell命令以查看输出:

代码:echo shell_exec('ls /tmp'); …

php file-put-contents

5
推荐指数
0
解决办法
917
查看次数

如何使用 file_get_contents() 加载远程文件?

现在有点抓我的头了。
我正在尝试从远程 url 获取文件。我可以从本地文件中保存内容,但不能从远程文件中保存。

这有效:

file_put_contents(
    'file1.xml',
    file_get_contents('file2.xml')
);
Run Code Online (Sandbox Code Playgroud)

这不会:

file_put_contents(
    'file1.xml',
    file_get_contents('http://www.domain.com/xmlapi/search.aspx?query=places&lid=38')
);
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我的其他脚本在第一次尝试时运行正常,但现在没有人会访问远程 url。
我最近开始在 Heart Internet 上托管我的新 php 文件。我在其他服务器上有类似的脚本,完全没有问题。

这是我得到的错误:

警告:file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sites/mydomain/myfile.php on line 5`

警告:file_get_contents(http://www.domain.com/xmlapi/search.aspx?query=places&lid=38)[function.file-get-contents]:无法打开流:php_network_getaddresses:getaddrinfo 失败:名称或服务不是在第 5 行的 /home/sites/mydomain/public_html/myfile.php 中已知`

allow_url_fopen 被设定为 On

不同的域

php file-get-contents file-put-contents

3
推荐指数
1
解决办法
8291
查看次数

有没有更好的方法循环这些 PHP 代码,也许使用 foreach 循环?

file_get_contents用来获取远程定价(每天更新),用于substr仅保留我想要的部分(从输出中去除货币符号和其他数据并仅保留数字)并用于file_put_contents将其存储到我引用的缓存目录中到以后。

这就是我现在所拥有的:-

<?php

$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

// Small Plan - US
$cachefile_SM_US = $cacheDirectory . 'SM_US.cache';

if(file_exists($cachefile_SM_US)) {
    if(time() - filemtime($cachefile_SM_US) > 1600) {
        // too old , re-fetch
        $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');
        $substr_SM_US = substr($cache_SM_US,17,2);
        file_put_contents($cachefile_SM_US, $substr_SM_US);
        } else {
            // cache is still fresh
    }
} else {
    // no cache, create one
    $cache_SM_US = file_get_contents('https://remotedomain.com/?get=price&product=10&currency=1');
    $substr_SM_US = substr($cache_SM_US,17,2);
    file_put_contents($cachefile_SM_US, $substr_SM_US);
}

// Large Plan - US
$cachefile_LG_US = $cacheDirectory . …
Run Code Online (Sandbox Code Playgroud)

php foreach loops file-get-contents file-put-contents

3
推荐指数
1
解决办法
77
查看次数