Subiendo archivo a través de formulario html. En PHP, $ _FILES está vacío

Entonces tengo este código:

<form name="form" action="./" method="post">
<input type="file" name="newfile" id="newfile" />
</form>

When I run this php script on the action page, nothing happens

echo "$_FILES['newfile']['name'];
print_r($_FILES);

It is like the $_FILES variable is empty. Is there any way to fix this?

preguntado el 08 de noviembre de 11 a las 18:11

3 Respuestas

Necesita configurar el enctype="multipart/form-data" atributo en su formulario.

Example 1 from php.net:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

respondido 08 nov., 11:22

Si no especifica <form … enctype="multipart/form-data"> then the data will be encoded in a way that doesn't support files.

Add the attribute with that value.

Desde la especificación HTML:

enctype = content-type [CI]

This attribute specifies the content type used to submit the form to the server (when the value of method is "post"). The default value for this attribute is "application/x-www-form-urlencoded". The value "multipart/form-data" should be used in combination with the INPUT element, type="file".

Desde el manual de PHP:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">

respondido 08 nov., 11:22

echo "$_FILES['newfile']['name'];

That's a syntax error. Should be

echo $_FILES['newfile']['name']; 

or

echo "{$_FILES['newfile']['name']}";

Respondido 17 ago 16, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.