PHP多个文件上传,名称更改

时间:2015-12-08 12:48:08

标签: php

我从w3学校获得了这个代码并且看起来很正常,我很难改变上传文件的描述。 到第一个是上传发票。 第二次上传股票表 第三次上传POD 第四张上传图片。

任何帮助将不胜感激。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Basic Uploader</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
</head>

<body>

<h2>Cases</h2>

<?php


$full_path = "c:/scott/source/";

# Set how many simultaneous uploads to allow.

$number_of_uploads = 4;

# Set allowed file types, lowercase without period (.)

$allowed_file_types =    array("jpg","gif","png","txt","zip","xls","doc","docx","xlsx");

# Change the upload_folder path, with trailing slash, 
# to your full directory path if neccessary, and set 
# permissions (chmod) to 777.

$upload_folder = "./uploads/";

# Set maximum file upload size in kilobytes.

$max_size_in_kb = 1024;

# Set 1 to rename files, 0 to keep original file names.

$rename_files = 1;

# END SETTING VARIABLES #################

function printForm()
{
global $allowed_file_types,$number_of_uploads,$max_size_in_kb;

print "<form action=\"". htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES) ."\" method=\"post\" enctype=\"multipart/form-data\">\n";

    for($i=0;$i<$number_of_uploads;$i++)
    {
    print "<p><input type=\"file\" name=\"file[]\" /></p>\n";
    }

print "<p><input type=\"hidden\" name=\"upload\" value=\"1\" /><input type=\"submit\" value=\"Upload\" /></p>\n</form>\n";

print "<p>Allowed file types: ." . implode($allowed_file_types, " ."). "</p>\n";
}

$fileNAMES = array();

if(isset($_POST['upload']))
{
    for($i=0;$i<$number_of_uploads;$i++)
    {
        if(strlen($_FILES['file']['name'][$i]) > 0)
        {
        $filearray = explode(".", $_FILES['file']['name'][$i]);
        $ext = end($filearray);

            if($rename_files == 1)
            {
            list($usec, $sec) = explode(" ", microtime());
            $fileNAMES[$i] = $sec."_".$usec;
            }
            else
            {
            $xperiods = str_replace("." . $ext, "", $_FILES['file']['name'][$i]);
            $fileNAMES[$i] = str_replace(".", "", $xperiods);
            }

            if(!in_array(strtolower($ext), $allowed_file_types))
            {
            print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: File type not allowed.</p>\n";
            }
            elseif($_FILES['file']['size'][$i] > ($max_size_in_kb*1024))
            {
            print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: File size to large.</p>\n";
            }
            elseif(file_exists($upload_folder.$fileNAMES[$i] .".". $ext))
            {
            print "<p class=\"error\">FAILED: ". htmlspecialchars($fileNAMES[$i]) .".". $ext ."<br />ERROR: File already exists.</p>\n";
            }
            else
            {
                if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_folder.$fileNAMES[$i] .".". $ext))
                {
                print "<p>UPLOADED: ". htmlspecialchars($fileNAMES[$i]) .".". $ext ."</p>\n";
                }
                else
                {
                print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: Undetermined.</p>\n";
                }
            }
        }
    }
printForm();
}
else
{
printForm();
}

?>

</body>
</html>

另外还有生成的文件名我希望将前一页的帖子放在前面。 $ CASE_ID

此致

1 个答案:

答案 0 :(得分:0)

试试这个

       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">

  <head>
     <title>Basic Uploader</title>
       <meta name="viewport" content="width=device-width, initial-scale=1" />
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <link rel="stylesheet" href="style.css" type="text/css" media="all" />
       </head>

      <body>

       <h2>Cases</h2>

        <?php


       $full_path = "c:/scott/source/";

        # Set how many simultaneous uploads to allow.

     $number_of_uploads = 4;

  # Set allowed file types, lowercase without period (.)

     $allowed_file_types =    array("jpg","gif","png","txt","zip","xls","doc","docx","xlsx");

   # Change the upload_folder path, with trailing slash, 
    # to your full directory path if neccessary, and set 
     # permissions (chmod) to 777.

    $upload_folder = "./uploads/";

   # Set maximum file upload size in kilobytes.

   $max_size_in_kb = 1024;

   # Set 1 to rename files, 0 to keep original file names.

   $rename_files = 1;

    # END SETTING VARIABLES #################

  function printForm()
    {
   global $allowed_file_types,$number_of_uploads,$max_size_in_kb;

   print "<form action=\"". htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES) ."\" method=\"post\" enctype=\"multipart/form-data\">\n";

  for($i=0;$i<$number_of_uploads;$i++)
  {
  print "<p><input type=\"file\" name=\"file[]\" /></p>\n";
  }

 print "<p><input type=\"hidden\" name=\"upload\" value=\"1\" /><input type=\"submit\" value=\"Upload\" /></p>\n</form>\n";

   print "<p>Allowed file types: ." . implode($allowed_file_types, " ."). "</p>\n";
    }

    $fileNAMES = array();

      if(isset($_POST['upload']))
    {
     for($i=0;$i<$number_of_uploads;$i++)
       {
       if(strlen($_FILES['file']['name'][$i]) > 0)
     {
     $filearray = explode(".", $_FILES['file']['name'][$i]);
    $ext = end($filearray);

        if($rename_files == 1)
        {
         if($i==0)  
         {  
          $fileNAMES[$i] = "upload invoice";
         }
         if($i==1)  
         {  
          $fileNAMES[$i] = "upload stocksheet";
         }
         if($i==2)  
         {  
          $fileNAMES[$i] = "upload POD";
         }
         if($i==3)  
         {  
          $fileNAMES[$i] = "upload picture";
         }
        }
        else
        {
        $xperiods = str_replace("." . $ext, "", $_FILES['file']['name'][$i]);
        $fileNAMES[$i] = str_replace(".", "", $xperiods);
        }

        if(!in_array(strtolower($ext), $allowed_file_types))
        {
        print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: File type not allowed.</p>\n";
        }
        elseif($_FILES['file']['size'][$i] > ($max_size_in_kb*1024))
        {
        print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: File size to large.</p>\n";
        }
        elseif(file_exists($upload_folder.$fileNAMES[$i] .".". $ext))
        {
        print "<p class=\"error\">FAILED: ". htmlspecialchars($fileNAMES[$i]) .".". $ext ."<br />ERROR: File already exists.</p>\n";
        }
        else
        {
            if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_folder.$fileNAMES[$i] .".". $ext))
            {
            print "<p>UPLOADED: ". htmlspecialchars($fileNAMES[$i]) .".". $ext ."</p>\n";
            }
            else
            {
            print "<p class=\"error\">FAILED: ". htmlspecialchars($_FILES['file']['name'][$i]) ."<br />ERROR: Undetermined.</p>\n";
            }
        }
    }
    }
  printForm();
   }
   else
    {
      printForm();
  }

  ?>