2

我已经为此苦苦挣扎了一段时间,希望有人能指出我正确的方向。我有一个用于上传单个图像的脚本。我现在正在尝试修改它,以便我可以一次更新多个文件。2 在下面的例子中。我知道 name 需要是一个数组,我遍历它们但是我似乎只是遇到了错误。我已经阅读并尝试了各种不同的东西。

我要么能够上传一个文件,但不能上传第二个文件,不上传任何文件或空白屏幕。

以下是经过各种编辑后我目前正在使用的内容。

      <?php

    $upload_dir= 'training/trainingDocuments';
    $numUploads = 2;

    $msg = 'Please select files for uploading';
    $errors = array();

    if(isset($_FILES['myTrainingFile']['tmp_name']))
    {
        for($i=0; $i < count($_FILES['myTrainingFile']['tmp_name']);$i++)
        {
            $fileName = $_FILES['myTrainingFile']['name'][$i];
            $tempName = $_FILES['myTrainingFile']['tmp_name'][$i];
            $blacklist = array('exe','php','jsp','js','bat','asp','aspx','com','dmg');
            $a = explode('.', $fileName);
            $fileExt  = strtolower(end($a)); unset($a);
            $fileSize = $_FILES['myTrainingFile']['size'];

            if(in_array($fileExt, $blacklist) === true){
                    $errors[] = "File type not allowed";
            }

            //$newPath = $general->fileNewPath($path, $fileName);

            $newPath = "training/trainingDocuments/" . $_FILES['myTrainingFile']['name'][$i];
            $moveFileResult = move_uploaded_file($tempName, $newPath);
            if($moveFileResult != true){
                $errors[] = 'Upload Failed - MOVE';
            }

            $comments = htmlentities(trim($_POST['comments']));
            $category = htmlentities(trim($_POST['category']));

            //insert into db
            $training->uploadDocument($fileName, $category, $comments);


            if(!is_uploaded_file($_FILES['myTrainingFile']['name'][$i]))
            {
                $errors[] = 'Uploading '.$_FILES['myTrainingFile']['name'][$i] . 'failed -.-';
            }


        }
    }
    ?>

谢谢你的帮助!

4

1 回答 1

2

试试这段代码,我添加了一个名为 的函数convert_files,这样你就可以更好地处理你的上传

代码:

<?php
  $upload_dir = "training/trainingDocuments";
  $numUploads = 2;

  $msg    = "Please select file(s) for uploading";
  $errors = array();

  // how many files you want to upload
  $maxFiles = 3;

  if ( $files = convert_files( $_FILES["myTrainingFile"] ) ) {
    foreach( $files as $i => $file ) {
      $fileName = $file["name"];
      $tempName = $file["tmp_name"];
      $fileSize = $file["size"];
      // get file extension, and do strtolower
      $fileExt  = strtolower( pathinfo( $fileName, PATHINFO_EXTENSION ) );
      // invalid file types
      $blacklist  = array( 'exe','php','jsp','js','bat','asp','aspx','com','dmg' );
      // new path to upload current file
      $newPath  = "training/trainingDocuments/".$fileName;

      // Check whether its a valid file or invalid file
      if ( in_array( $fileExt, $blacklist ) ) {
        // invalid file type, add error
        $errors[$i] = "File type not allowed";
      }

      if ( !is_uploaded_file( $tempName ) ) {
        // its'' not an uploaded file, add error
        $errors[$i] = "Uploading ".$fileName." failed -.-";
      }

      if ( file_exists( $newPath ) ) {
        // file already exists in your directory, add error
        $errors[$i] = "File ".$fileName." already exists";
        // if you dont want to add error, (adding an error will not upload file)
        // just comment above line, and uncomment below line

        /*
        // get the filename without extension
        $name = pathinfo( $fileName, PATHINFO_FILENAME );
        //create new file name
        $fileName = $name."_".uniqid().".".$fileExt;
        //update upload path
        $newPath  = "training/trainingDocuments/".$fileName;
        */
      }

      // make sure $errors[$i] contains any errors
      if ( isset( $errors[$i] ) ) {
        // errors occured, so continue to next file
        continue;
      }

      if ( !move_uploaded_file( $tempName, $newPath ) ) {
        $errors[$i] = "Upload Failed - MOVE"; // failed to move file
      }

      $comments = htmlentities( trim( $_POST['comments'] ) );
      $category = htmlentities( trim( $_POST['category'] ) );

      // Upload document
      $training->uploadDocument( $fileName, $category, $comments );

      // check maximum allowed files limit exceeded
      if ( ( $i + 1 ) == $maxFiles ) {
        // limit exceeded, break the execution
        break;
      }
    }
  }
?>

功能:

<?php
  function convert_files( $files ) {
    if ( is_array( $files ) && !empty( $files["name"] ) ) {
      if ( is_array( $files["name"] ) ) {
        $merged = array();
        foreach( $files["name"] as $i => $name ) {
          $merged[] = array(
            "name"  =>  $name,
            "type"  =>  $files["type"][$i],
            "size"  =>  $files["size"][$i],
            "error" =>  $files["error"][$i],
            "tmp_name"  =>  $files["tmp_name"][$i]
          );
        }
        return $merged;
      }
      return array( $files );
    }
    return false;
  }
?>
于 2013-07-24T11:03:49.027 回答