将包含file_put_contents的文件保存在文件夹中 [英] Save file with file_put_contents in folder

查看:279
本文介绍了将包含file_put_contents的文件保存在文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

file_put_contents('image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));

将文件保存在当前文件夹中就可以了,但是如果我尝试

Saving the file in current folder is working ok, but if I try

file_put_contents('/subfolder/image.jpg',file_get_contents('http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg'));

我有一个错误:

无法打开流:[…]中没有这样的文件或目录

failed to open stream: No such file or directory in […]

为什么会出现此错误?如何将文件保存在子文件夹中?

Why does this error occur? How can I save the file in a subfolder?

推荐答案

始终使用完整路径,并确保目录可写.您也可以直接将copy与URL

Always use full paths and make sure the directory is writable. You can also use copy directly with URL

$url = 'http://static.adzerk.net/Advertisers/12f0cc69cd9742faa9c8ee0f7b0d210e.jpg';
$dir = __DIR__ . "/subfolder"; // Full Path
$name = 'image.jpg';

is_dir($dir) || @mkdir($dir) || die("Can't Create folder");
copy($url, $dir . DIRECTORY_SEPARATOR . $name);

这篇关于将包含file_put_contents的文件保存在文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆