3

我是 php 的初学者,现在正在用 php 做一个项目。我想上传图片(最多四个图片文件)。我使用以下代码上传图片。

<script type="text/javascript">
count=1;
function add_file_field()
{
if(count<4)
{
var container=document.getElementById('file_container');
var file_field=document.createElement('input');
file_field.name='images[]';
file_field.type='file';
container.appendChild(file_field);
var br_field=document.createElement('br');
container.appendChild(br_field);
count++;
}
}

</script>



        <div id="file_container">
    <input name="images[]" type="file" id="file[]" />
<br />
    </div> 

        <br><a href="javascript:void(0);" onClick="add_file_field();">Add</a> 

我使用以下代码进行单个文件上传。它正在工作

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "File Error : " . $_FILES["file"]["error"] . "<br />";
}else {
echo "Upload File Name: " . $_FILES["file"]["name"] . "<br />";
echo "File Type: " . $_FILES["file"]["type"] . "<br />";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; 
 echo "File Description:: ".$_POST['description']."<br />";

 if (file_exists("images/".$_FILES["file"]["name"]))
 {
 echo "<b>".$_FILES["file"]["name"] . " already exists. </b>";
 }else
 {

$tmpname=$_FILES["file"]["tmp_name"];
$name=$_FILES["file"]["name"];
$new="jun.jpg";
rename($name,$new);
 move_uploaded_file($_FILES["file"]["tmp_name"],"images/".$new);
  echo "Stored in: " . "images/" .$new."<br />";
 ?>
 Uploaded File:<br>
 <img src="images/<?php echo $new; ?>" alt="Image path Invalid" >
<?php
}
}
}else
{
echo "Invalid file detail ::<br> file type ::".$_FILES["file"]["type"]." , file size:::       ".$_FILES["file"]["size"];
}
?>

我需要帮助来修改此代码以上传最多 4 张图片。可以使用重命名功能重命名所选文件以在移动到指定文件夹时上传吗?但它显示错误

请帮帮我

4

4 回答 4

3

您应该允许在文件输入中选择多个文件,因此您不必一遍又一遍地添加新输入:

<input id="file" type="file" name="images[]" multiple>

提交表单后,您可以像这样遍历$_FILES数组:

foreach($_FILES['images'] as $file) {
    //your code here --> replace $_FILES['file'] with $file
}

我希望这有帮助。

于 2013-09-04T07:33:09.223 回答
2
<?php


if(isset($_POST['submit']))
{

    $count=count($_FILES["images"]["name"]);


for($i=0;$i<$count;$i++)
{   
  if ((($_FILES["images"]["type"][$i] == "image/gif")
 || ($_FILES["images"]["type"][$i]  == "image/jpeg")
|| ($_FILES["images"]["type"][$i]  == "image/pjpeg"))
&& ($_FILES["images"]["size"][$i] < 100000))
{

 if ($_FILES["images"]["error"][$i]  > 0)
 {
 echo "File Error : " . $_FILES["images"]["error"][$i]  . "<br />";
  }
  else 
 {
  echo "Upload File Name: " . $_FILES["images"]["name"][$i]  . "<br />";
 echo "File Type: " . $_FILES["images"]["type"][$i]  . "<br />";
  echo "File Size: " . ($_FILES["images"]["size"][$i]  / 1024) . " Kb<br />"; 

   if (file_exists("public/images/".$_FILES["images"]["name"][$i] ))
  {
   echo "<b>".$_FILES["images"]["name"][$i]  . " already exists. </b>";
   }
   else
  {
    move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/".             $_FILES["images"]["name"][$i] );
    echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />";
   ?>
   Uploaded File:<br>
   <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path    Invalid" >
  <?php
  }
  }
  }else
 {
  echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." ,    file    size::: ".$_FILES["images"]["size"][$i] ;
  } 
}
   }?>

使用 for loop..foreach 上传的图像显示错误

于 2013-09-04T15:48:21.997 回答
1

下面是一个PHP中多文件上传的例子

https://github.com/hemantrai88/html5-php_multi-file-upload

于 2013-12-23T08:24:27.740 回答
0

我有一个有效的例子,我认为这会对你有所帮助

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" />
    <input type="submit"/>
</form>

在php中

if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       
        $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
        $desired_dir="user_data";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
         mysql_query($query);           
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
于 2013-09-04T10:41:42.873 回答