PHP中的多个文件上载 - 重命名文件

时间:2013-09-04 07:26:32

标签: php file file-upload rename

我是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 个答案:

答案 0 :(得分:3)

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

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

提交表单后,您可以迭代$_FILES数组:

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

我希望这会有所帮助。

答案 1 :(得分: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上传的图片显示错误

答案 2 :(得分:1)

以下是PHP中多文件上传的示例

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

答案 3 :(得分: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";
    }
}