Click to See Complete Forum and Search --> : How does it get the file name?


cusimar9
07-24-2007, 04:38 AM
I'm trying to get my head around someone else's code... and learn PHP at the same time.

I've got a simple file upload, but I can't see where its working out what the file name is.


// adds a news item to the database

if (!isset($submit))
{
echo "<form name='frmAdd' enctype='multipart/form-data' action='news_admin.php' method='post' onSubmit=\"return ValidateNews(document.frmAdd.title.value, document.frmAdd.news_text.value);\">";
echo "<table>";
echo "<tr><td><b><u>News Headline</u>:</b></td><td><input type='text' name='title' size='35'></td></tr>";
echo "<tr><td><b><u>News Text</u>:</b></td><td><textarea name='news_text' cols='35' rows='4'></textarea></td></tr>";
echo "<tr><td><b>Image File:</b></td><td><input type='file' name='imagefile'></td></tr>";
echo "</table>";
echo "<input type='hidden' name='mode' value='add'>";
echo "<input type='submit' name='submit' value='Add' title='Add News Item'>";
echo "</form>";
}
else
{
echo "THE FILE IS " . $imagefile_name;
}


The 'imagefile' and 'imagefile_name' names I've just made up, they're not mentioned anywhere else in the page. Yet the imagefile_name actually does hold the file name when the page is posted and a file uploaded. Is this a feature of PHP or is the code doing something I don't understand?

hastx
07-24-2007, 09:57 AM
when you submit a form, the browser sends the input elements of the form in an array of POST variables, attached files also have info regarding their attributes (such as name, filesize, etc.) in an array.

somewhere in your processing script, if you wanted to set $imagefile_name to be the name of the uploaded file....you would referance "$_FILES['imagefile']['name']"

Depending how your script is set up, you could do:

$imagefile_name = $_FILES['imagefile']['name'];

//files also have a 'temp' name which is a random name php uses while the file is in a working state.
//This is why you have to tell the file where to go once it is uploaded:
//if you dont state where to put the file and what to call it, it will be deleted after the script execution.

move_uploaded_file($_FILES['imagefile']['tmp_name'], $imagefile_name)

cusimar9
07-25-2007, 05:02 AM
That's the thing, the $_FILES variable isn't called ANYWHERE. I've searched through every page of code and the only reference to a $_FILES variable is entirely unrelated to this page.

MrCoder
07-25-2007, 06:44 AM
You have register globals set on and an input field has the name matching the variables name??


Image File:</b></td><td><input type='file' name='imagefile'>