Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get name from a <input type="file" /> ? 2

Status
Not open for further replies.

C0FFEE123

Programmer
Sep 1, 2008
44
0
0
NL
How can i get the name of <input type="file" /> ?
I tought with $_FILES['name']; but that wasn't correct.\
$_POST['name'] also doesn't works.
 
You must specify a name tag:
Code:
<input type="file" name="uploadfile" />

Then:
Code:
$_FILES['uploadfile']['name']

|| ABC
 
Code:
$_FILES['uploadedfile']['name']
Where "uploadedfile" is the name of the input.

-----------------------------------------
I cannot be bought. Find leasing information at
 
I just did it.
Only it doesn't works
I get Array[name]....
And i want the name of the input (for example picture.jpg)
 
can you post the actual html you're using, and
Code:
print_r($_FILES['uploadedfile']['name']);

-----------------------------------------
I cannot be bought. Find leasing information at
 
Your HTML code should be something like:

Code:
<form enctype="multipart/form-data" method="POST" action="...">
<input type="file" name="uploadfile" />
</form>
where "uploadfile" can be replaced with any name you wish (as long as you use the same name in the $_FILES).

Also, try replacing $_FILES with $HTTP_POST_FILES in case you are using a version of PHP earlier than 4.1.0

|| ABC
 
if you have more than one input with the same name, the results will be in a numerically ordered array.

please read the manual on handling file uploads
 
The PHP part send.php
include_once '../includes/db.inc.php';
include_once 'admin.inc.php';
check_login();
top_admin('occasions', $pagina_titel);
open_db();
$sql="INSERT INTO occasion (occasion_name, occasion_short_description, occasion_long_description, occasion_price, occasion_picture, occasion_picture_medium)
VALUES
('$_POST[artikel]', '$_POST[korte_omschrijving]', '$_POST[uitgebreide_omschrijving]', '$_POST[prijs]', '$HTTP_POST_FILES[plaatje][name]', '$HTTP_POST_FILES[plaatje_m][name]')";
echo $sql . "<br />" . "<br />";
mysql_query($sql);


The html part insert.php
//echo $_POST;

include_once '../includes/db.inc.php';
include_once 'admin.inc.php';

check_login();
top_admin('occasions', $pagina_titel);
open_db();
echo '<form action="occ_send.php" method="post" enctype="multipart/form-data"> <br />';
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="plaatje_m"> <br />';

echo '<input type="submit" value="Invoeren"';
 
There is also a input file for "plaatje"
Forgot to copy
 
I just tested this on my server. here is the code
Code:
<?php
echo '<form action="test2.php" method="post" enctype="multipart/form-data"> <br />'; ?>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="plaatje"> <br />
<?php
echo   '<input type="submit" value="Invoeren">';
?>
and
Code:
<?php
print_r($_POST);
echo "<br/>";
echo $_FILES[plaatje][name] . '<br/>';
echo $_FILES['plaatje']['name'];
?>
the result is
Code:
Array ( [MAX_FILE_SIZE] => 1000000 ) 
crash.JPG
crash.JPG

-----------------------------------------
I cannot be bought. Find leasing information at
 
I know that is also working at mine.
Only i have to put it into a database :')
So i can't use the echo.
I get Array['name'] the whole time.
I want to see picture.jpg in there! :p
 
read the manual.

the $_FILES array does NOT hold the file itself. but pointers to the file. the file is stored in the location specified in temp_name. you get the contents of the file through one of the normal file_* commands.

do not forget to escape db data. you are leaving yourself open to sql injection attacks.

 
That part, what is it???
>[tt][prijs]', [red]'$HTTP_POST_FILES[plaatje][name]',[/red] '$HTTP_POST_FILES[plaatje_m][name]')";[/tt]
 
Also the field name is quoted, not bared.
 
I now made it:
Code:
$sql="INSERT INTO occasion (occasion_name, occasion_short_description, occasion_long_description, occasion_price, occasion_picture, occasion_picture_medium)
VALUES
('$_POST[artikel]', '$_POST[korte_omschrijving]', '$_POST[uitgebreide_omschrijving]', '$_POST[prijs]', $_FILES[plaatje][name], '$_FILES[plaatje_m][name]')";
 
Code:
$sql="INSERT INTO occasion (occasion_name, occasion_short_description, occasion_long_description, occasion_price, occasion_picture, occasion_picture_medium)
VALUES
('$_POST[artikel]', '$_POST[korte_omschrijving]', '$_POST[uitgebreide_omschrijving]', '$_POST[prijs]', '" . $_FILES[plaatje][name] . "', '" .$_FILES[plaatje_m][name]. "')";

-----------------------------------------
I cannot be bought. Find leasing information at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top