dongxiong1941 2016-02-03 21:00
浏览 31
已采纳

在PHP中为上传的文档创建循环

I am looking to create a loop for uploading several documents (49 to be exact!!!). I am completely new to loops so any direction or help would be great. Here is my current code:

$folder="uploads/";

    if(isset($_FILES["t1url"])) {
        $t1urlextension = end(explode(".", $_FILES["t1url"]["name"]));
        $t1urlname = "M1s1t1url";
        $t1urldoc_loc = $_FILES['t1url']['tmp_name'];
        move_uploaded_file($t1urldoc_loc,$folder.$t1urlname.".".$t1urlextension);
    }

    if(isset($_FILES["t2url"])) {
        $t2urlextension = end(explode(".", $_FILES["t2url"]["name"]));
        $t2urlname = "M1s1t2url";
        $t2urldoc_loc = $_FILES['t2url']['tmp_name'];
        move_uploaded_file($t2urldoc_loc,$folder.$t2urlname.".".$t2urlextension);
    }

I have this working for these 2 but the thought of changing this over and over again 49 times seems brutal.

Essentially there are 10 locations that would need to increase by one

if(isset($_FILES["t1url"])) {

"t1url" to "t2url" to "t3url" etc.

$t1urlextension =

"$t1urlextension" to "$t2urlextension" to "$t3urlextension" to etc.

end(explode(".", $_FILES["t1url"]["name"]));

"t1url" to "t2url" to "t3url" to etc.

$t1urlname

"$t1urlname" to "$t2urlname" to "$t3urlname" to etc.

"M1s1t1url"

"M1s1t1url" to "M1s1t2url" to "M1s1t3url" to etc.

$t1urldoc_loc

"$t1urldoc_loc" to "$t2urldoc_loc" to "$t3urldoc_loc" to etc.

$_FILES['t1url']['tmp_name'];

"t1url" to "t2url" to "t3url" to etc.

move_uploaded_file($t1urldoc_loc,$folder.$t1urlname.".".$t1urlextension);

"$t1urldoc_loc" to "$t2urldoc_loc" to "$t3urldoc_loc" to etc.

"$t1urlname" to "$t2urlname" to "$t3urlname" to etc.

"$t1urlextension" to "$t2urlextension" to "$t3urlextension" to etc.

Thanks for the help in advance!

UPDATE

Here is the solution I was able to come up with based on JTC's answer:

    $url = array("t1url","t2url");
    $extension = array("t1ext","t2ext");
    // $name = array("t1name","t2name");
    $tab = array("M1s1t1url","M1s1t2url");
    $loc = array("t1loc","t2loc");
    $folder="Uploads/";
    $arrlength = count($url);

    for($x = 0; $x < $arrlength; $x++) {
        if(isset($_FILES[$url[$x]])) {
            $extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
            // $name[$x] = $tab[$x];
            $loc[$x] = $_FILES[$url[$x]]['tmp_name'];
            move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
        }
    }

Works perfectly!!! Thanks!

UPDATE

I spoke to soon! I can get this to work for 4 but anything over 4 will not work. Here is the code that works:

    $url = array(
        "t1url",
        "t2url",
        "t3url",
        "t4url");
    $extension = array(
        "t1ext",
        "t2ext",
        "t3ext",
        "t4ext");
    $tab = array(
        "M1s1t1url",
        "M1s1t2url",
        "M1s1t3url",
        "M1s1t4url");
    $loc = array(
        "t1loc",
        "t2loc",
        "t3loc",
        "t4loc");
    $folder="Uploads/";
    $arrlength = count($url);

    for($x = 0; $x < $arrlength; $x++) {
        if(isset($_FILES[$url[$x]])) {
            $extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
            $loc[$x] = $_FILES[$url[$x]]['tmp_name'];
            move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
        }
    }

This for some reason does not work:

    $url = array(
        "t1url",
        "t2url",
        "t3url",
        "t4url",
        "t5url");
    $extension = array(
        "t1ext",
        "t2ext",
        "t3ext",
        "t4ext",
        "t5ext");
    $tab = array(
        "M1s1t1url",
        "M1s1t2url",
        "M1s1t3url",
        "M1s1t4url",
        "M1s1t5url");
    $loc = array(
        "t1loc",
        "t2loc",
        "t3loc",
        "t4loc",
        "t5loc");
    $folder="Uploads/";
    $arrlength = count($url);

    for($x = 0; $x < $arrlength; $x++) {
        if(isset($_FILES[$url[$x]])) {
            $extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
            $loc[$x] = $_FILES[$url[$x]]['tmp_name'];
            move_uploaded_file($loc[$x],$folder.$tab[$x].".".$extension[$x]);
        }
    }

Here is the error message I am receiving in the code that does not work:

I get this 5 times:

Strict standards: Only variables should be passed by reference in C:\wamp2\www\phpKiosk\mc1.php on line 535

Here is line 535:

$extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));

I get this error once:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp2\www\phpKiosk\mc1.php:535) in C:\wamp2\www\phpKiosk\mc1.php on line 773

Here is line 773:

header("location: http://localhost/phpkiosk/mc1.php");

Any help would be awesome. I am really new to this and I have to be overlooking something simple.

  • 写回答

1条回答 默认 最新

  • dtf76989 2016-02-03 21:04
    关注

    You can create an array and the loop over it.

    $array = array("file1,file2,file3,file4");
    
    foreach($array as $item){
     // your upload code
    }
    

    But more elegant way to upload your files would be using glob like this.

    $files = glob('upload/*'); // get all file names
    foreach($files as $file){ // iterate files
      if(is_file($file))
        // your upload script
    }
    

    Update: Based on your problem I would try to use this:

    $path = $_FILES[$url[$x]]["name"];
    $extension[$x] = pathinfo($path, PATHINFO_EXTENSION);
    

    Instead of this:

    $extension[$x] = end(explode(".", $_FILES[$url[$x]]["name"]));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog