【问题标题】:file_puts_content permission denied on Windows 7 WAMPWindows 7 WAMP 上的 file_puts_content 权限被拒绝
【发布时间】:2014-05-27 15:53:20
【问题描述】:

我正在尝试使用本地网站上的 file_put_contents 从网站下载一些图像并将它们保存到 C:\ 驱动器。但是在运行脚本时出现错误

file_put_contents(C:\product_images\A): failed to open stream: Permission denied

我对 product_images 文件夹以及其中的 A 文件夹拥有完全权限。

我知道我可以在 ubuntu 中进行 chmod,但不确定我可以用 Windows 做什么。我只是右键单击并选择了属性,并确保所有用户都应用了所有权限

    public function showImage() {

    $product_image = ['/ABCD/ABCD.jpg','/ABCDE/ABCDE.jpg'];

    foreach($product_image as $product_images) {
        $url = "http://images.url.com/product_images" . $product_images  ."";
        $letter = substr($product_images, 1, 1);
        $folder = 'C:\product_images\ ' .$letter . '';
        $new_folder = str_replace(' ', '', $folder);
        file_put_contents($new_folder, file_get_contents($url));
    }

    $success = "Success!";

    return $success;
}

【问题讨论】:

  • file_put_contents() 不创建文件夹。您需要在保存文件之前创建文件夹。
  • 完全正确。该文件夹必须存在。如果您尝试保存文件,请尝试添加扩展名,例如 .jpg
  • 是的,A 文件夹确实存在。但是,如果我删除该文件夹,它只会创建一个 A 文件,我认为该文件不是文件格式,因为它具有文件大小。
  • 所以我希望文件像 product_images\A\ABCD.jpg 一样保存

标签: php permissions wamp


【解决方案1】:
    foreach($product_image as $product_images) {
        $url = "http://images.com/product_images" . $product_images  ."";

        $test = explode("/", $product_images);

        $folder = 'C:\product_images\ ' . $test[2] . '';

        $new_folder = str_replace(' ', '', $folder);

        $newer_folder = str_replace('/', '\\', $new_folder);

        file_put_contents($newer_folder, file_get_contents($url));

        echo '../product_images/' . $test[2] . '';
        echo '<br />';
    }

【讨论】:

  • 我可以用explode()搞清楚
【解决方案2】:

file_put_contents 仅在allow_url_fopen 开启(“1”)时才可用于 URL。用

检查它
ini_get("allow_url_fopen");

还要注意,虽然您的写作文件夹可能具有所需的权限,但 父文件夹 可能没有。两个(可能的)文件夹都应该有权限。

您还可以测试文件夹是否可写(通过is_writable

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 2018-02-12
    • 2014-08-22
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2011-05-13
    • 2013-07-10
    相关资源
    最近更新 更多