开发者

PHP pass a var into $_FILES inside function

I am working on an admin area that will have file uploads on several pages. Regardless of the name of the form field I want to do some specific processes, b开发者_运维知识库ut I'm a bit stuck on how to do this in a function. I have the code below, which I knew going in wouldn't be correct, but I'm not sure how to achieve what I'm trying to do.

function fileprocessor ($file)
{
    echo $_FILES['$file']['tmp_name'];
}

Any thoughts?


function fileprocessor ($file)
{
    echo $_FILES[$file]['tmp_name'];
}

From manual:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.


$_FILES is the process of a form submit. to pass it to a function you need to pass the field name through the function.

function fileprocessor($file) {
 $name = $_FILES[$file]['tmp_name'];
 echo $name;
}

In the Code you will have

fileprocessor('userfile'); # Enter field name from upload form

This way you can have what ever you may like in the file processor

EDIT

You can test my version here http://jawilliams.site11.com/filestest.php


That's quite common mistake.
Many newbie programmers take single quotes as a part of array syntax. While it's merely string delimiters and has actually nothing to do with arrays.
This mistake can lead to some funny or dramatic consequences.

So, to use a variable just use this variable, without quotes or other symbols.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜