【问题标题】:PHP multiple uploads with PHP functions使用 PHP 函数进行 PHP 多次上传
【发布时间】:2018-06-19 15:02:15
【问题描述】:

我尝试了解如何在 PDO 中使用多个上传。

所以我有我的 post.php:

$database = new Database();
$db = $database->getConnection(); 
$media = new Media($db);

if($_POST){

 $image = !empty($_FILES["image"]["name"]) ? sha1_file($_FILES['image']['tmp_name']) . "-" .  time() .  "-" . basename($_FILES["image"]["name"]) : ""; 
 $media->image = $image;    

   if ($media->create()) {
     echo $media->uploadPhoto();
     $_POST=array();
   }
}

 <input name="image[]" type="file" />

我的 media.php 带有 pdo 查询和一些上传验证:

  public $image; 
  public $created;  

    public function create(){

    //write query
    $query = "INSERT INTO " . $this->table_name . "
    SET image=:image, 
    created=:created";

    $stmt = $this->conn->prepare($query);

    $this->image=htmlspecialchars(strip_tags($this->image));
    $stmt->bindParam(":image", $this->image);

    if($stmt->execute()){
        return true;
    }

    print_r($stmt->errorInfo());
    return false;
 }
 function uploadPhoto(){

    $result_message="";

    if(!empty($_FILES["image"]["tmp_name"])){
        $target_directory = "../uploads/";
        $target_file = $target_directory . $this->image;
        $file_type = pathinfo($target_file, PATHINFO_EXTENSION);
        $file_upload_error_messages="";
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check!==false){
        }else{
        $file_upload_error_messages.="<div></div>";
        }
        $allowed_file_types=array("jpg", "JPG", "jpeg", "JPEG", "png", "PNG", "gif", "GIF");
        if(!in_array($file_type, $allowed_file_types)){
           $file_upload_error_messages.="<div></div>";
        }
        if(file_exists($target_file)){
        }
        if($_FILES['image']['size'] > (10485760)){
        $file_upload_error_messages.="<div></div>";
        }
        if(!is_dir($target_directory)){
           mkdir($target_directory, 0777, true);
        }
        if(empty($file_upload_error_messages)){
            if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
            }else{
                $result_message.="<div></div>";
            }
        }
        else{
           $result_message.="{$file_upload_error_messages}";
        }
    }
    return $result_message;
}

所以我需要将值存储在不同的mysql记录中,并将它们全部上传到服务器。

执行此操作的最佳方法是什么以及将 foreach 放置在哪里? 我在 foreach 的正确位置上挣扎...

$x=0;  
foreach ( $_FILES['image']['name'] AS $key => $value ){  
    // ...
    $x++;  
}

【问题讨论】:

  • 请正确格式化您的问题并添加所有相关代码。您还应该告诉我们实际问题是什么。你到底卡在哪里了?示例:您正在调用 $media-&gt;create() 来执行一些数据库操作,但由于您没有发布完整的方法,因此我们看不到什么。你也没有向那个函数传递任何东西。
  • 你在你的create 函数中缺少一个"(也许只是在这里,但你应该更好地格式化代码)
  • 我添加了相关代码。我不清楚如何使用函数 uploadPhoto() 插入多个(而不仅仅是一个)图像。
  • 当您已经拥有 PDO 时,您的第一个错误是制作数据库包装器。仅仅因为你使用它,这并不意味着你的代码神奇地面向对象。

标签: php pdo foreach


【解决方案1】:

因此,如果这是您的 foreach 循环,并且您想为每个上传的文件插入不同的记录,请使用 mysqli_multi_query

  $x=0;  
  $query = "";
  foreach ( $_FILES['image']['name'] AS $key => $value ){  
     $query .= 'insert into tableName (col1,col2,col3,image)VALUES("a","b","c","'.$value.'");';
     $x++;  
  }
  $query = rtrim($query,";");
  mysqli_multi_query($connectionObj,$query);

你的查询看起来像这样

insert into tableName (col1,col2,col3,image)VALUES("a","b","c","a.jpg");
insert into tableName (col1,col2,col3,image)VALUES("a","b","c","b.jpg");

【讨论】:

  • 这没有使用 PDO(阅读:php.net/manual/en/pdo.prepared-statements.php
  • 好吧,当所有执行完成后,您可以在循环中使用我的逻辑执行查询,然后不要关闭连接,因为它会减慢进程
  • 好吧,我明白了,我要重写我的代码......我想把它放在 oop/function 中,但似乎我必须把所有东西都写在同一个文件中。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 2017-02-24
  • 2011-06-11
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2013-07-07
相关资源
最近更新 更多