dongyuan1970 2015-01-17 21:21
浏览 17
已采纳

如何在函数中使用$ _FILES?

I have some code that is supposed to upload a file and move it to the correct folder:

$check = getimagesize($files['image']['tmp_name']);
    if($check !== false)
    {
      $dir = "/var/www/public_html/images/";
      $file = $dir.basename($files['image']['name']);
      if(file_exists($file)) 
      {
        $img = "https://example.com/images/".basename($files['image']['name']);
      }
      else
      {
        move_uploaded_file($files['image']['tmp_name'], $file);
        $img = "https://example.com/images/".basename($files['image']['name']);
      }
    }

The issue is that this code is in a function (and a different file), so I cannot directly use $_FILES. In my other file, where the post occurs, I have this:

Images::editImage($post, $_FILES)

It works for post, but not for files. Is there any way to carry $_FILES across files?

  • 写回答

1条回答 默认 最新

  • dongpin1850 2015-01-17 21:49
    关注

    The statement

    The issue is that this code is in a function (and a different file), so I cannot directly use $_FILES.

    is false. $_FILES as a superglobal variable can be used everywhere in a script, class method, function, ...

    So, if "... not working" is occurring, the problem might either be that it is not populated due to plainly missing an uploaded file, or access to it's array elements is wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部