duanhai7274 2015-11-21 03:52
浏览 43

如何在数组中存储$ _FILES超全局变量并循环遍历它们以在PHP中找到匹配项?

I'm running if statements to identify each variable independently. My code is a bit messy and long but it works with no problem. I'd like to see if it is possible to use an array to store them and run a loop to see if there is a match, instead of using if statements.

PHP code...

if (move_uploaded_file($_FILES["index_deslizador_Cfile1"]["tmp_name"], $index_deslC1)) {
     echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Cfile2"]["tmp_name"], $index_deslC2)) {
     echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Cfile3"]["tmp_name"], $index_deslC3)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Cfile4"]["tmp_name"], $index_deslC4)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Mfile1"]["tmp_name"], $index_deslM1)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Mfile2"]["tmp_name"], $index_deslM2)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Mfile3"]["tmp_name"], $index_deslM3)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }
else if (move_uploaded_file($_FILES["index_deslizador_Mfile4"]["tmp_name"], $index_deslM4)) {
    echo "<script>window.open('../index.php','_self')</script>";
        }                   
else {
    echo "Sorry, there was an error uploading your file.";
     }
  • 写回答

1条回答 默认 最新

  • duanlvxi8652 2015-11-21 04:39
    关注

    I'm fairly new to PHP, so I may get scolded for this suggestion :) If you could get $index_deslC# variables into an array, you might be able to do something like this:

    $i = 1
    $destination = array($index_deslC...
    foreach($_FILES["index_deslizador_Mfile" . $i]["tmp_name"] as $filename) {
        isset(move_uploaded_file($filename, $destination[$i]) ?
        echo "<script>window.open('../index.php','_self')</script>" : echo '';
        $i++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题