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.

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

报告相同问题?