<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Using remote files</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Caracteristici" HREF="features.html"><LINK REL="PREVIOUS" TITLE="PUT method support" HREF="features.file-upload.put-method.html"><LINK REL="NEXT" TITLE="Connection handling" HREF="features.connection-handling.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="features.file-upload.put-method.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="features.connection-handling.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="features.remote-files" >Cap. 21. Using remote files</A ></H1 ><P > As long as <VAR CLASS="option" >allow_url_fopen</VAR > is enabled in <TT CLASS="filename" >php.ini</TT >, you can use <ACRONYM CLASS="acronym" >HTTP</ACRONYM > and <ACRONYM CLASS="acronym" >FTP</ACRONYM > URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the <A HREF="function.include.html" ><B CLASS="function" >include()</B ></A >, <A HREF="function.include-once.html" ><B CLASS="function" >include_once()</B ></A >, <A HREF="function.require.html" ><B CLASS="function" >require()</B ></A > and <A HREF="function.require-once.html" ><B CLASS="function" >require_once()</B ></A > statements. See <A HREF="wrappers.html" >Anexa L</A > for more information about the protocols supported by PHP. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > In PHP 4.0.3 and older, in order to use URL wrappers, you were required to configure PHP using the configure option <VAR CLASS="option" >--enable-url-fopen-wrapper</VAR >. </P ></BLOCKQUOTE ></DIV ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > The Windows versions of <VAR CLASS="literal" >PHP</VAR > earlier than PHP 4.3 did not support remote file accessing for the following functions: <A HREF="function.include.html" ><B CLASS="function" >include()</B ></A >, <A HREF="function.include-once.html" ><B CLASS="function" >include_once()</B ></A >, <A HREF="function.require.html" ><B CLASS="function" >require()</B ></A >, <A HREF="function.require-once.html" ><B CLASS="function" >require_once()</B ></A >, and the imagecreatefromXXX functions in the <A HREF="ref.image.html" >Referinþã XLII, <I >Image Functions</I ></A > extension. </P ></BLOCKQUOTE ></DIV > </P ><P > 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. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4407" ></A ><P ><B >Exemplu 21-1. Getting the title of a remote page</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$file </font><font color="#007700">= </font><font color="#0000BB">fopen </font><font color="#007700">(</font><font color="#DD0000">"http://www.example.com/"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$file</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"<p>Unable to open remote file.\n"</font><font color="#007700">;<br /> exit;<br />}<br />while (!</font><font color="#0000BB">feof </font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$line </font><font color="#007700">= </font><font color="#0000BB">fgets </font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#0000BB">1024</font><font color="#007700">);<br /> </font><font color="#FF8000">/* This only works if the title and its tags are on one line */<br /> </font><font color="#007700">if (</font><font color="#0000BB">eregi </font><font color="#007700">(</font><font color="#DD0000">"<title>(.*)</title>"</font><font color="#007700">, </font><font color="#0000BB">$line</font><font color="#007700">, </font><font color="#0000BB">$out</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$title </font><font color="#007700">= </font><font color="#0000BB">$out</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">];<br /> break;<br /> }<br />}<br /></font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A > call will fail. </P ><P > To connect as a user other than 'anonymous', you need to specify the username (and possibly password) within the URL, such as 'ftp://user:password@ftp.example.com/path/to/file'. (You can use the same sort of syntax to access files via HTTP when they require Basic authentication.) </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN4414" ></A ><P ><B >Exemplu 21-2. Storing data on a remote server</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$file </font><font color="#007700">= </font><font color="#0000BB">fopen </font><font color="#007700">(</font><font color="#DD0000">"ftp://ftp.example.com/incoming/outputfile"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$file</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"<p>Unable to open remote file for writing.\n"</font><font color="#007700">;<br /> exit;<br />}<br /></font><font color="#FF8000">/* Write the data here. */<br /></font><font color="#0000BB">fwrite </font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">, </font><font color="#0000BB">$_SERVER</font><font color="#007700">[</font><font color="#DD0000">'HTTP_USER_AGENT'</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">);<br /></font><font color="#0000BB">fclose </font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > 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 <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A > call will fail if the remote file already exists. To do distributed logging like that, you should take a look at <A HREF="function.syslog.html" ><B CLASS="function" >syslog()</B ></A >. </P ></BLOCKQUOTE ></DIV > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="features.file-upload.put-method.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="features.connection-handling.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PUT method support</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="features.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Connection handling</TD ></TR ></TABLE ></DIV ></BODY ></HTML >