【问题标题】:Upload and display multiple images using php?使用php上传并显示多张图片?
【发布时间】:2016-02-07 09:08:12
【问题描述】:

插入代码:

 <input type="hidden" name="size" value="350000">
<input type="file" name="photo">

显示代码:

 echo "<img src='/images/" . $row['photo']. "' height='150px'><br>";

但这只能处理一张照片。有什么方法可以制作多张图片?

这是处理输入到表单中的数据的 php:

 <?php

 //This is the directory where images will be saved
 $target = "images/";
 $target = $target . basename( $_FILES['photo']['name']);

 //This gets all the other information from the form
 $photo=($_FILES['photo']['name']);

 // Connects to your Database
 mysql_connect("localhost", "", "") or die(mysql_error()) ;
 mysql_select_db("dirtypol_election2016compariso") or die(mysql_error()) ;

 // Writes the information to the database
 mysql_query("INSERT INTO databasename (photo) VALUES ('$photo')") ;

 //Writes the photo to the server
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {

 //Tells you if its all ok
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
 }
 else {
 //Gives and error if its not
 echo "Sorry, there was a problem uploading your file.";
 }
 ?>

【问题讨论】:

  • 循环播放!使用任何循环语句来处理多个图像。但更清楚你想做什么!?
  • 您显示的两个 sn-ps 代码似乎无法协同工作 - 您肯定还有其他代码没有显示?
  • 我用我用来处理数据的 php 更新了帖子。

标签: php image file-upload image-uploading multiple-file-upload


【解决方案1】:
  • 输入名称必须定义为数组,即 name="inputName[]"

  • 输入元素必须有多个=“多个”或只有多个在您的

  • PHP 文件使用语法“$_FILES['inputElemName']['param'][index]”

  • 确保查找空文件名和路径,数组可能
    包含空字符串

HTML:

<input name="files[]" type="file" multiple="multiple" />

PHP:

<?php

include('config.php'); // my database connection is come form this file

// check files are set or not
if(isset($_FILES['files'])){

    $errors= array();
    $desired_dir="attachments/"; // replace with your directory name where you want to store images
    // getting files array
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

        $file_name = $_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        // checking file size (because i use it)
        if($file_size > 8097152){
            $errors[]='File size must be less than 8 MB'; // change or remove it
        }       

        $filename=time().$file_name; // for creating unique file name
        if(empty($errors)==true){
            // moving files to destination
            $move=move_uploaded_file($file_tmp,$desired_dir.$filename);
            // you can direct write move_uploaded files method in bellow if condition
            if($move)
            {
                // inserting data into database
                mysql_query("INSERT into your_tablename(your_field) VALUES('$filename')"); // inserting data if file is moved
                echo "The file ".$filename." has been uploaded"; // only for debugging
            }
            else{
                echo $filename."is note uploaded"; // use this for debugging otherwise remove it
            }

        }
        else{
            echo $errors;
        }
            }
    }
    if(empty($error)){
        echo "All attachments are uploaded successfully"; // your success message/action
    }
?>

我正在使用上面的代码上传和插入 multipal 文件的数据,它工作顺利。 :) 希望这有帮助...

【讨论】:

  • 我已经用我用来处理数据的 php 更新了帖子。你能检查我的 php 代码,看看我可以如何更新或改进它吗?
  • 嘿,Thomas,我更新了我的 php 代码...检查一下...希望这对您有用。它对我有用。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多