खोज…


वाक्य - विन्यास

  • हर स्ट्रीम में एक योजना और एक लक्ष्य होता है:
  • <योजना>: // <लक्ष्य>

पैरामीटर

मापदण्ड नाम विवरण
स्ट्रीम संसाधन डेटा प्रदाता जिसमें <scheme>://<target> वाक्यविन्यास शामिल है

टिप्पणियों

धाराएं मूल रूप से एक मूल और गंतव्य के बीच डेटा का हस्तांतरण हैं, जोश लॉकहार्ट को अपनी पुस्तक मॉडर्न PHP में।

उत्पत्ति और गंतव्य हो सकता है

  • एक पंक्ति
  • एक कमांड-लाइन प्रक्रिया
  • एक नेटवर्क कनेक्शन
  • एक ज़िप या टीएआर संग्रह
  • अस्थायी स्मृति
  • मानक इनपुट / आउटपुट

या PHP के स्ट्रीम रैपर के माध्यम से उपलब्ध कोई अन्य संसाधन।

उपलब्ध स्ट्रीम रैपर ( schemes ) के उदाहरण:

  • फ़ाइल: // - स्थानीय फाइल सिस्टम तक पहुँच
  • http: // - HTTP (s) URL तक पहुँचना
  • FTP: // - एफ़टीपी (यूआरएल) तक पहुँचने
  • php: // - विभिन्न I / O स्ट्रीम एक्सेस करना
  • phar: // - PHP आर्काइव
  • ssh2: // - सुरक्षित शैल 2
  • ogg: // - ऑडियो स्ट्रीम

स्कीम (मूल) स्ट्रीम के रैपर की पहचानकर्ता है। उदाहरण के लिए, फ़ाइल सिस्टम के लिए यह file:// । लक्ष्य स्ट्रीम डेटा स्रोत है, उदाहरण के लिए फ़ाइल नाम।

एक स्ट्रीम रैपर रजिस्टर करना

एक स्ट्रीम रैपर एक या अधिक विशिष्ट योजनाओं के लिए एक हैंडलर प्रदान करता है।

नीचे दिया गया उदाहरण एक सरल स्ट्रीम आवरण दिखाता है जो स्ट्रीम बंद होने पर PATCH HTTP अनुरोध भेजता है।

// register the FooWrapper class as a wrapper for foo:// URLs.
stream_wrapper_register("foo", FooWrapper::class, STREAM_IS_URL) or die("Duplicate stream wrapper registered");

class FooWrapper {
    // this will be modified by PHP to show the context passed in the current call.
    public $context;

    // this is used in this example internally to store the URL
    private $url;

    // when fopen() with a protocol for this wrapper is called, this method can be implemented to store data like the host.
    public function stream_open(string $path, string $mode, int $options, string &$openedPath) : bool {
        $url = parse_url($path);
        if($url === false) return false;
        $this->url = $url["host"] . "/" . $url["path"];
        return true;
    }

    // handles calls to fwrite() on this stream
    public function stream_write(string $data) : int {
        $this->buffer .= $data;
        return strlen($data);
    }

    // handles calls to fclose() on this stream
    public function stream_close() {
        $curl = curl_init("http://" . $this->url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->buffer);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
        curl_exec($curl);
        curl_close($curl);
        $this->buffer = "";
    }

    // fallback exception handler if an unsupported operation is attempted.
    // this is not necessary.
    public function __call($name, $args) {
        throw new \RuntimeException("This wrapper does not support $name");
    }

    // this is called when unlink("foo://something-else") is called.
    public function unlink(string $path) {
        $url = parse_url($path);
        $curl = curl_init("http://" . $url["host"] . "/" . $url["path"]);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_exec($curl);
        curl_close($curl);
    }
}

यह उदाहरण केवल कुछ उदाहरण दिखाता है कि जेनेरिक स्ट्रीम आवरण में क्या होगा। ये सभी तरीके उपलब्ध नहीं हैं। लागू होने वाली विधियों की पूरी सूची http://php.net/streamWrapper पर देखी जा सकती है।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow