douchun1948 2013-06-25 22:32
浏览 29

在PHP中重用代码(函数?)

Got this bit of code:

if ($_FILES["file1"]["error"] == 0) {
        move_uploaded_file($_FILES["file1"]["tmp_name"], "path/".$_FILES["file1"]["name"]);
    }

I would like to reuse it for more files being uploaded. Was thinking function with some params but it seems i can't get the vars correctly.

Ok fixed it like this:

function upload($file) {

        $allowedExts = array("pdf, jpg, gif, png");
        $extension = end(explode(".", $_FILES[$file]["name"]));

        if (in_array($extension, $allowedExts)) && ($_FILES[$file]["error"] == 0) {
            move_uploaded_file($_FILES[$file]["tmp_name"], "img/new/".$_FILES[$file]["name"]);
        }
    }

And calling via:

upload("file1");

Not sure about the $_FILES loop...

  • 写回答

3条回答 默认 最新

  • doulu4203 2013-06-25 22:35
    关注

    You can loop through the $_FILES array and execute your code for each file, like this:

    foreach($_FILES as $file)
    {
        if ($file["error"] == 0) {
            move_uploaded_file($file["tmp_name"], "path/".$file["name"]);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度