php file_put_contents 的错误子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/519964/
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
error clause for file_put_contents
提问by Joshxtothe4
I am using this line to obtain and save an image from a URL.
我正在使用这一行从 URL 获取和保存图像。
file_put_contents("./images/".$pk.".jpg", file_get_contents($PIC_URL))
I am unsure what the best way to deal with an error is. At the moment it is failing because there is no permission, which will be remedied shortly, but I would like it to be able to deal with situations where PIC_URL is empty or not an image. Should I dead with the error at this level(probably it is better for permission related things) or should I check higher up if PIC_URL is empty, or both?
我不确定处理错误的最佳方法是什么。目前它失败了,因为没有权限,很快就会修复,但我希望它能够处理 PIC_URL 为空或不是图像的情况。如果 PIC_URL 为空,我是否应该因这个级别的错误而死(对于与权限相关的事情来说可能更好),或者我应该检查更高的位置,还是两者兼而有之?
Which is the best approach?
哪种方法最好?
回答by chuckg
I'm not talented enough to claim this is the best method, but I would just test along the way:
我没有足够的天赋来声称这是最好的方法,但我只是一路测试:
$imageDir = "/path/to/images/dir/";
$imagePath = "$imageDir$pk.jpg";
if (!is_dir($imageDir) or !is_writable($imageDir)) {
// Error if directory doesn't exist or isn't writable.
} elseif (is_file($imagePath) and !is_writable($imagePath)) {
// Error if the file exists and isn't writable.
}
$image = file_get_contents(urlencode($PIC_URL));
if (empty($image)) {
// Error if the image is empty/not accessible.
exit;
}
file_put_contents($imagePath, $image);
回答by benlumley
I would use is_writable(), passing it the folder name if the image doesn't already exist, or the filename if it does, before you attempt to write the image.
我会使用 is_writable(),如果图像不存在,则将文件夹名称传递给它,如果存在,则在尝试写入图像之前传递文件名。
回答by Gabriel Sosa
try making a function for this.
尝试为此创建一个函数。
<?php
define('OK', 0);
deinfe('URL_EMPTY', 1);
define('WRITING_PROBLEMS',2);
define('OTHER_PROBLEM', 3);
function save_pic($pic_url) {
$imageDir = '/path/to/images/dir/';
if (!strlen($pic_url))
return URL_EMPTY;
if (!is_dir($imageDir) || !is_writable($imageDir)) {
return WRITING_PROBLEMS;
}
$image = file_get_contents(urlencode($pic_url));
$pk = time(); // or whatever you want as key
$r = file_put_contents($imagePath.$pk.".jpg", $pic_url);
if ($r)
return OK;
else
return OTHER_PROBLEM;
}
?>
回答by Jeff Alan Midler
Hmm couldn't you also just do
嗯,你不能也只是做
file_put_contents($file, $str) or die("Unable to write file!");
file_put_contents($file, $str) or die("Unable to write file!");
回答by cg.
Both, as far as I am concern. Especially with those dangerous file handling functions, double-checking doesn't hurt. (Where does $pkcome from?)
两者,就我而言。特别是对于那些危险的文件处理函数,双重检查并没有什么坏处。($pk从哪里来?)
Generally, check higher up for nicer feedback to the user and check right before you execute for security. Its hard to give decent feedback to the user when only checking at a low level. On the other hand, it is hard to check for all possible errors (like those file system permissions) on a high and generic level.
通常,请向上级检查以获得更好的用户反馈,并在执行之前检查以确保安全。仅在低级别检查时很难向用户提供体面的反馈。另一方面,很难在高级和通用级别上检查所有可能的错误(例如那些文件系统权限)。

