2002-04-15 00:12:54 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2005-11-18 16:25:23 +00:00
|
|
|
<!-- $Revision: 1.7 $ -->
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- splitted from ./en/functions/filesystem.xml, last change in rev 1.39 -->
|
|
|
|
<refentry id="function.is-uploaded-file">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>is_uploaded_file</refname>
|
|
|
|
<refpurpose>Tells whether the file was uploaded via HTTP POST</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>bool</type><methodname>is_uploaded_file</methodname>
|
|
|
|
<methodparam><type>string</type><parameter>filename</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2003-02-11 03:04:15 +00:00
|
|
|
Returns &true; if the file named by <parameter>filename</parameter> was
|
2002-04-15 00:12:54 +00:00
|
|
|
uploaded via HTTP POST. This is useful to help ensure that a
|
|
|
|
malicious user hasn't tried to trick the script into working on
|
|
|
|
files upon which it should not be working--for instance,
|
|
|
|
<filename>/etc/passwd</filename>.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This sort of check is especially important if there is any chance
|
|
|
|
that anything done with uploaded files could reveal their
|
|
|
|
contents to the user, or even to other users on the same
|
|
|
|
system.
|
|
|
|
</para>
|
2005-02-14 16:20:00 +00:00
|
|
|
<para>
|
|
|
|
For proper working, the function <function>is_uploaded_file</function> needs
|
|
|
|
an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded
|
|
|
|
file on the clients machine $_FILES['userfile']['name'] does not work.
|
|
|
|
</para>
|
|
|
|
<example>
|
|
|
|
<title><function>is_uploaded_file</function> example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
|
|
|
|
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
|
|
|
|
echo "Displaying contents\n";
|
|
|
|
readfile($_FILES['userfile']['tmp_name']);
|
|
|
|
} else {
|
|
|
|
echo "Possible file upload attack: ";
|
|
|
|
echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
|
|
|
|
}
|
|
|
|
|
|
|
|
?>]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
|
|
|
<function>is_uploaded_file</function> is available only in
|
|
|
|
versions of PHP 3 after PHP 3.0.16, and in versions of PHP 4
|
|
|
|
after 4.0.2. If you are stuck using an earlier version, you can
|
|
|
|
use the following function to help protect yourself:
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The following example will <emphasis>not</emphasis> work in
|
|
|
|
versions of PHP 4 after 4.0.2. It depends on internal
|
|
|
|
functionality of PHP which changed after that version.
|
|
|
|
</para>
|
|
|
|
</note>
|
2004-05-18 14:23:19 +00:00
|
|
|
</para>
|
|
|
|
<example>
|
2005-02-14 16:20:00 +00:00
|
|
|
<title><function>is_uploaded_file</function> example for PHP 4 < 4.0.3</title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/* Userland test for uploaded file. */
|
2005-11-18 16:25:23 +00:00
|
|
|
function is_uploaded_file_4_0_2($filename)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2002-04-15 00:12:54 +00:00
|
|
|
if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
|
|
|
|
$tmp_file = dirname(tempnam('', ''));
|
|
|
|
}
|
|
|
|
$tmp_file .= '/' . basename($filename);
|
|
|
|
/* User might have trailing slash in php.ini... */
|
|
|
|
return (ereg_replace('/+', '/', $tmp_file) == $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is how to use it, since you also don't have
|
|
|
|
* move_uploaded_file() in these older versions: */
|
2005-11-18 16:25:23 +00:00
|
|
|
if (is_uploaded_file_4_0_2($HTTP_POST_FILES['userfile'])) {
|
2002-04-15 00:12:54 +00:00
|
|
|
copy($HTTP_POST_FILES['userfile'], "/place/to/put/uploaded/file");
|
|
|
|
} else {
|
|
|
|
echo "Possible file upload attack: filename '$HTTP_POST_FILES[userfile]'.";
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2004-05-18 14:23:19 +00:00
|
|
|
</example>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
|
|
|
See also <function>move_uploaded_file</function>, and the section
|
|
|
|
<link linkend="features.file-upload">Handling file uploads</link>
|
|
|
|
for a simple usage example.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<!-- 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:"../../../../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
|
|
|
|
-->
|