File: remote-files.xml

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (134 lines) | stat: -rw-r--r-- 4,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297028 $ -->
 <chapter xml:id="features.remote-files" xmlns="http://docbook.org/ns/docbook">
  <title>Using remote files</title>

  <para>
   As long as <option>allow_url_fopen</option> is enabled in
   &php.ini;, you can use <acronym>HTTP</acronym> and <acronym>FTP</acronym> 
   URLs with most of the functions
   that take a filename as a parameter.  In addition, URLs can be
   used with the <function>include</function>,
   <function>include_once</function>, <function>require</function> and
   <function>require_once</function> statements (since PHP 5.2.0,
   <option>allow_url_include</option> must be enabled for these).
   See <xref linkend="wrappers"/> for more information about the protocols
   supported by PHP.
  </para>
  <note>
   <para>
   In PHP 4.0.3 and older, in order to use URL wrappers, you were required
   to configure PHP using the configure option
   <option>--enable-url-fopen-wrapper</option>.
   </para>
  </note>
  <para>
   <note>
    <para>
     The Windows versions of PHP earlier than PHP 4.3
     did not support remote file accessing for the following functions:
     <function>include</function>, <function>include_once</function>,
     <function>require</function>, <function>require_once</function>,
     and the imagecreatefromXXX functions in the <xref linkend="ref.image"/>
     extension.
    </para>
   </note>
  </para>
  <para>
   For example, you can use this to open a file on a remote web server,
   parse the output for the data you want, and then use that data in a
   database query, or simply to output it in a style matching the rest
   of your website.
  </para>
  <para>
   <example>
    <title>Getting the title of a remote page</title>
    <programlisting role="php">
<![CDATA[
<?php
$file = fopen ("http://www.example.com/", "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
}
while (!feof ($file)) {
    $line = fgets ($file, 1024);
    /* This only works if the title and its tags are on one line */
    if (preg_match ("@\<title\>(.*)\</title\>@i", $line, $out)) {
        $title = $out[1];
        break;
    }
}
fclose($file);
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   You can also write to files on an FTP server (provided that you
   have connected as a user with the correct access rights). You
   can only create new files using this method; if you try to overwrite
   a file that already exists, the <function>fopen</function> call will
   fail.
  </para>
  <para>
   To connect as a user other than 'anonymous', you need to specify
   the username (and possibly password) within the URL, such as
   '<literal>ftp://user:password@ftp.example.com/path/to/file</literal>'.
   (You can use the same sort of syntax to access files via
   <acronym>HTTP</acronym> when they require Basic authentication.)
  </para>
  <para>
   <example>
    <title>Storing data on a remote server</title>
    <programlisting role="php">
<![CDATA[
<?php
$file = fopen ("ftp://ftp.example.com/incoming/outputfile", "w");
if (!$file) {
    echo "<p>Unable to open remote file for writing.\n";
    exit;
}
/* Write the data here. */
fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");
fclose ($file);
?>
]]>  
    </programlisting>
   </example>
  </para>
  <para>
   <note>
    <para>
     You might get the idea from the example above that you can use
     this technique to write to a remote log file. Unfortunately
     that would not work because the <function>fopen</function> call will
     fail if the remote file already exists. To do distributed logging
     like that, you should take a look at <function>syslog</function>.
    </para> 
   </note>
  </para>

 </chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->