【问题标题】:Having a hard time with PHP arrays很难使用 PHP 数组
【发布时间】:2012-08-06 08:02:01
【问题描述】:

我正在尝试为我的网站制作文件上传脚本,但我不知道如何从我的数组中获取 namesize 值(我不太擅长数组)。

我可以让它与上传单个文件一起工作,但我怎样才能让它循环呢?

我需要将它上传到我的服务器,并且它还在数据库中创建了一个条目。

这是我的代码...

HTML:

<form method="post" action="" enctype="multipart/form-data">
  <textarea name="reply_message"></textarea>
  <input name="attachments[]" type="file" size="20">
  <input type="submit" name="ReplyForm" value="Send">
</form>

PHP:

if(!empty($_POST['reply_message']))
{
    $reply_messages = $_POST['reply_message'];

    $database->query('INSERT INTO email_response (email_id, message) VALUES (?, ?)', array($email_id, $reply_message));

    if(isset($_FILES['attachments']))
    {
        require("upload.class.php");
        $upload = new upload();

        $last_email_response_id = $database->lastInsertId();
        $attachments = $_FILES['attachments'];

        foreach($attachments as $key => $value)
        {
            print_r($attachments);

            $database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_response_id, $attachments['name'][0]));

            $last_attachment_id = $database->lastInsertId();

            $upload->set('attachments', ATTACHMENTS.$reply_result->sender_email.'/'.$email_id.'/'.$last_attachment_id);

            $upload->upload();
        }
    }
}

数组(上传两个文件):

Array (
  [name] => Array (
    [0] => linkdownarrow.gif
    [1] => overlay-button.png
  )
  [type] => Array (
    [0] => image/gif
    [1] => image/png
  )
  [tmp_name] => Array (
    [0] => F:\xampp\tmp\phpFEC0.tmp
    [1] => F:\xampp\tmp\phpFEC1.tmp
  )
  [error] => Array (
    [0] => 0
    [1] => 0
  )
  [size] => Array (
    [0] => 63
    [1] => 135
  )
)

【问题讨论】:

    标签: php arrays file upload pdo


    【解决方案1】:
    if(count($_FILES['attachments']) > 0) {
        for($i = 0; $i < count($_FILES['attachments']); $i++) {
            move_uploaded_file($_FILES['attachments']['tmp_name'][$i], 'new/path');
            echo 'The name of the file is' . $_FILES['attachments']['name'][$i];
        }
        // or
        foreach($_FILES['attachments'] as $attachment) {
            echo $attachment['name'];
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的;

      $img_types = array('image/jpeg', 'image/pjpeg');
      $img_count = count($_FILES['attachments']['name']);
      for($i = 0; $i < $img_count; $i++) {
          $tmp_name = $_FILES['attachments']['tmp_name'][$i];
      
          // forget empty file[] fields
          if($tmp_name == '') {
              continue;
          }
      
          // or more secure way to check allowed file types
          if(in_array($_FILES['attachments']['type'][$i], $img_types)) {
              // resize_images() etc...
          }
      }
      

      【讨论】:

        【解决方案3】:

        另一种方式:你可以重写 $_FILES 数组...

            $attachments = array();
        
            if(count($_FILES['attachments']) > 0)
            {
              for($i = 0; $i < count($_FILES['attachments']); $i++)
              {
                $attachments[] = array(
                  'name' => $_FILES['attachments']['name'][$i],
                  'size' => $_FILES['attachments']['size'][$i],
                  'tmp_name' => $_FILES['attachments']['tmp_name'][$i],
                  'type' => $_FILES['attachments']['type'][$i],
                  'error' => $_FILES['attachments']['error'][$i],
                );
              }
            }
        
            print_r($attachments);
        

        输出将如下所示:

        Array (
          [0] => Array (
            ['name'] => linkdownarrow.gif
            ['type'] => image/gif
            ['tmp_name'] => F:\xampp\tmp\phpFEC0.tmp
            ['error'] => 0
            ['size'] => 63
          )
          [1] => Array (
            ['name'] => overlay-button.png
            ['type'] => image/png
            ['tmp_name'] => F:\xampp\tmp\phpFEC1.tmp
            ['error'] => 0
            ['size'] => 135
          )
        )
        

        现在你可以使用它了:

            foreach($attachments as $attachment)
            {
                $database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_response_id, $attachment['name']));
                $last_attachment_id = $database->lastInsertId();
                $upload->set('attachments', ATTACHMENTS.$reply_result->sender_email.'/'.$email_id.'/'.$last_attachment_id);
                $upload->upload();
            }
        

        【讨论】:

        【解决方案4】:

        我得到它来处理这个......

        $attachments = $_FILES['attachments'];
        if(count($attachments['name']) > 0)
        {
            for($i = 0; $i < count($attachments['name']); $i++)
            {
                $database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, file_size, created) VALUES (?, ?, ?, ?, NOW())', array($email_id, $last_email_response_id, $attachments['name'][$i], $attachments['size'][$i]));
        
                $last_attachment_id = $database->lastInsertId();
        
                $upload->set('attachments', ATTACHMENTS.$reply_result->sender_email.DS.$email_id.DS.$last_attachment_id);
                $upload->upload();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          • 1970-01-01
          • 2021-05-30
          • 2017-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多