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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File upload as function

Status
Not open for further replies.

SuaveRick

Programmer
Apr 12, 2004
142
CA
Hi there. I'm trying to write a .inc file that contains a function that will upload a file. The problem is that I have to pass the $_FILES arrary to the function... do I just use $_FILES[] as a parameter and as a second parameter sent the actual value of the filename input box? So I would have the $_FILES[] array and the filename I need to reference all passed to the function... does this sound like it would work or does the file upload stuff have to be done outside a script?

This is how I am calling my function which is in an included file:
$returnvar=upload_single_file($_FILES[],$_POST[vlu]);

This is the function declaration:
function upload_single_file($filesarray,$filename {....}

and this line will not work in the function, I assume because it really does not have the $_FILES array...
basename($filesarray['$filename']['name']);

Is what I am trying to do even possible? Works if the code is not in a function and just in the php page.

Thanks,

Suave
 
Actually I don't think my $_FILES is worknig at all, I've been playing with it and I can't get ANY data from $_FILES....
 
$_FILES array is superglobal. That means that it is available everywhere in the script, including within functions. You are using your own array called $filesarray which is just a regular array and you would need to invoke global if you would want to pull the variable from outside of the function. $_FILES however, should be available anywhere. The same as $_POST, which you're also using. Consider this and maybe you'll find a much simpler solution.
 
On your second remark, have you tried:
Code:
echo '<pre>';
print_r($_FILES);
echo '</pre>';
 
Then I should be able to use $_FORMS anywhere, that's good.
Here is my HTML:

print "<html>";
print "<body>";

$form="<form method=\"post\" action=\"index.php\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
<input type=\"file\" name=\"vlu\"><br>
<input type=\"submit\" value=\"submit\">
</form>";

if($_POST[seenform]=="y")
{
echo '<pre>';
print_r($_FILES);
echo '</pre>';

}
else
{
print $form;
}
print "</body>";
print "</html>";

I use the browse button on the html to find my file then I submit, I get back:

Array
(
)

Is there something I should change in the php.ini file? I took a look in ther but nothing really stuck out at me....

Thanks!
 
You're missing the [tt]enctype="multipart/form-data"[/tt] as one of your form attributes. That attribute is necessary when you want to upload files to the server:
Code:
$form = "<form method=\"post\" action=\"index.php\" enctype=\"multipart/form-data\">
 
That's fixed it guys, thatnks very much. I'll have to do some reading on that because I had no idea you had to do that.

Have a good one!
 
I have it all working and it copies the file to the dir I want but when I click on the image to open it, it says no preview available... the original opens fine in MS pic viewer but this uploaded one does not. What could be causing this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top