简体   繁体   English

PHP:无法打开流:没有此类文件或目录

[英]PHP: Failed to open stream: No such file or directory

I'm working on implementing Safari Push Notification service for my website. 我正在为我的网站实施Safari推送通知服务。 I've been following the tutorial made by Apple which can be found here . 我一直在关注Apple制作的教程,可以在这里找到。 And we have to create a package to serve push notifications and Apple provides a companion php file that helps to create the package. 我们必须创建一个程序包来提供推送通知,Apple提供了一个随附的php文件来帮助创建程序包。

Now I've downloaded the php file and tried running it, but it always throws a warning saying "Failed to open stream: No such file or directory". 现在,我已经下载了php文件并尝试运行它,但是它总是抛出警告“无法打开流:没有这样的文件或目录”。

Full Log: 完整日志:

D:\...\Downloads\php-7.0.4-nts-Win32-VC14-x86>php D:\..\Downloads\Safari-Push-Notifications-master\createPushPackage.php
Warning: copy(pushPackage.raw/icon.iconset/icon_16x16.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_16x16@2x.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_32x32.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_32x32@2x.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30
Warning: copy(pushPackage.raw/icon.iconset/icon_128x128.png): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30 
Warning: copy(pushPackage.raw/icon.iconset/icon_128x128@2x.png): failed to openstream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line30
Warning: copy(pushPackage.raw/website.json): failed to open stream: No such fileor directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 30  

Warning: file_get_contents(D:/../Desktop/dntest1457346433/icon.iconset/icon_128x128@2x.png): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39     
.
.
.
Warning: file_get_contents(D:/../Desktop/dntest1457346433/icon.iconset/icon_16x16.png): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39  
Warning: file_get_contents(D:/../Desktop/dntest1457346433/website.json): failed to open stream: No such file or directory in ...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 39
Warning: file_get_contents(Desktop): failed to open stream: No such file or directory in D:\...\Downloads\Safari-Push-Notifications-master\createPushPackage.php on line 47

PS: I do have a directory named pushPackage.raw that contains a folder called icon.iconset which has a set of 6 png images. PS:我确实有一个名为pushPackage.raw的目录,其中包含一个名为icon.iconset的文件夹,该文件夹包含6张png图像。 So I have no idea why it says "No such file is present". 所以我不知道为什么它说“不存在这样的文件”。

PHP Companion file PHP伴侣文件

<?php

// This script creates a valid push package.
// This script assumes that the website.json file and iconset already exist. 
// This script creates a manifest and signature, zips the folder, and returns the push package. 

// Use this script as an example to generate a push package dynamically.


$certificate_path = "Desktop";     // Change this to the path where your certificate is located
$certificate_password = "pwd1"; // Change this to the certificate's import password

// Convenience function that returns an array of raw files needed to construct the package.
function raw_files() {
    return array(
        'icon.iconset/icon_16x16.png',
        'icon.iconset/icon_16x16@2x.png',
        'icon.iconset/icon_32x32.png',
        'icon.iconset/icon_32x32@2x.png',
        'icon.iconset/icon_128x128.png',
        'icon.iconset/icon_128x128@2x.png',
        'website.json'
    );
}

// Copies the raw push package files to $package_dir.
function copy_raw_push_package_files($package_dir) {
    mkdir($package_dir . '/icon.iconset');
    foreach (raw_files() as $raw_file) {
        copy("pushPackage.raw/$raw_file", "$package_dir/$raw_file");
    }
}

// Creates the manifest by calculating the SHA1 hashes for all of the raw files in the package.
function create_manifest($package_dir) {
    // Obtain SHA1 hashes of all the files in the push package
    $manifest_data = array();
    foreach (raw_files() as $raw_file) {
        $manifest_data[$raw_file] = sha1(file_get_contents("$package_dir/$raw_file"));
    }
    file_put_contents("$package_dir/manifest.json", json_encode((object)$manifest_data));
}

// Creates a signature of the manifest using the push notification certificate.
function create_signature($package_dir, $cert_path, $cert_password) {
    // Load the push notification certificate
    $pkcs12 = file_get_contents($cert_path);
    $certs = array();
    if(!openssl_pkcs12_read($pkcs12, $certs, $cert_password)) {
        return;
    }

    $signature_path = "$package_dir/signature";

    // Sign the manifest.json file with the private key from the certificate
    $cert_data = openssl_x509_read($certs['cert']);
    $private_key = openssl_pkey_get_private($certs['pkey'], $cert_password);
    openssl_pkcs7_sign("$package_dir/manifest.json", $signature_path, $cert_data, $private_key, array(), PKCS7_BINARY | PKCS7_DETACHED);

    // Convert the signature from PEM to DER
    $signature_pem = file_get_contents($signature_path);
    $matches = array();
    if (!preg_match('~Content-Disposition:[^\n]+\s*?([A-Za-z0-9+=/\r\n]+)\s*?-----~', $signature_pem, $matches)) {
        return;
    }
    $signature_der = base64_decode($matches[1]);
    file_put_contents($signature_path, $signature_der);
}

// Zips the directory structure into a push package, and returns the path to the archive.
function package_raw_data($package_dir) {
    $zip_path = "$package_dir.zip";

    // Package files as a zip file
    $zip = new ZipArchive();
    if (!$zip->open("$package_dir.zip", ZIPARCHIVE::CREATE)) {
        error_log('Could not create ' . $zip_path);
        return;
    }

    $raw_files = raw_files();
    $raw_files[] = 'manifest.json';
    $raw_files[] = 'signature';
    foreach ($raw_files as $raw_file) {
        $zip->addFile("$package_dir/$raw_file", $raw_file);
    }

    $zip->close();
    return $zip_path;
}

// Creates the push package, and returns the path to the archive.
function create_push_package() {
    global $certificate_path, $certificate_password;

    // Create a temporary directory in which to assemble the push package
    $package_dir = 'D:/Users/Mike/Desktop/dntest' . time();
    if (!mkdir($package_dir)) {
        unlink($package_dir);
        die;
    }

    copy_raw_push_package_files($package_dir);
    create_manifest($package_dir);
    create_signature($package_dir, $certificate_path, $certificate_password);
    $package_path = package_raw_data($package_dir);

    return $package_path;
}


// MAIN
$package_path = create_push_package();
if (empty($package_path)) {
    http_response_code(500);
    die;
}

header("Content-type: application/zip");
echo file_get_contents($package_path);
die;

Another thing, this php script does create a package at the specified location (D:/Users/Mike/Desktop), but without the contents present in the pushPackage.raw directory. 另一件事,此php脚本确实在指定位置(D:/ Users / Mike / Desktop)创建了一个程序包,但是pushPackage.raw目录中没有该内容。

Please help me resolve this issue. 请帮助我解决此问题。 Thank you so much! 非常感谢!

You need to run createPushPackage.php from the folder with Safari-Push-Notifications package (in your case D:\\..\\Downloads\\Safari-Push-Notifications-master\\ ) because code copy("pushPackage.raw/$raw_file", "$package_dir/$raw_file"); 您需要从带有Safari-Push-Notifications包(在您的情况下为D:\\..\\Downloads\\Safari-Push-Notifications-master\\ )的文件夹中运行createPushPackage.php,因为代码为copy("pushPackage.raw/$raw_file", "$package_dir/$raw_file"); use relative path to assets files. 使用资产文件的相对路径。

Add some explanation for relative paths: 添加一些有关相对路径的解释:

bash$ pwd
/var/www/test # we inside /var/www/
bash$ ls -la # there are 2 files
    -rw-r--r--   1 alexanderpolomodov  wheel   48 Mar  7 15:07 test.php
    -rw-r--r--   1 alexanderpolomodov  wheel   15 Mar  7 15:08 test.txt
# test.php consist from one string
# var_dump(file_get_contents('test.txt'));
bash$ php test.php # run this script
    string(15) "test filepaths"
bash$ cd ..
# when we run this command from another folder we get error
bash$ php test/test.php 
    Warning: file_get_contents(test.txt): failed to open stream: No such file or directory in /private/var/www/test/test.php on line 3
    bool(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM