Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 允许上载多个文件_Php_Html_File_Upload - Fatal编程技术网

Php 允许上载多个文件

Php 允许上载多个文件,php,html,file,upload,Php,Html,File,Upload,我在网上找到了一个脚本,我做了一些修改。我想允许一次上传五个文件。这是我的剧本: // Folder to upload files to. Must end with slash / define('DESTINATION_FOLDER','../uploads/'); // Maximum allowed file size, Kb // Set to zero to allow any size define('MAX_FILE_SIZE', 10240); // Upload suc

我在网上找到了一个脚本,我做了一些修改。我想允许一次上传五个文件。这是我的剧本:
// Folder to upload files to. Must end with slash /
define('DESTINATION_FOLDER','../uploads/');

// Maximum allowed file size, Kb
// Set to zero to allow any size
define('MAX_FILE_SIZE', 10240);

// Upload success URL. User will be redirected to this page after upload.
define('SUCCESS_URL','my info');

// Allowed file extensions. Will only allow these extensions if not empty.
// Example: $exts = array('avi','mov','doc');
$exts = array('jpg', 'jpeg', 'png', 'gif');

// rename file after upload? false - leave original, true - rename to some unique         filename
define('RENAME_FILE', true);

// put a string to append to the uploaded file name (after extension);
// this will reduce the risk of being hacked by uploading potentially unsafe files;
// sample strings: aaa, my, etc.
define('APPEND_STRING', '');

// Need uploads log? Logs would be saved in the MySql database.
define('DO_LOG', true);

// MySql data (in case you want to save uploads log)
define('DB_HOST','my info'); // host, usually localhost
define('DB_DATABASE','my info'); // database name
define('DB_USERNAME','my info'); // username
define('DB_PASSWORD','my info'); // password


/*CREATE TABLE uploads_log (
  log_id int(11) unsigned NOT NULL auto_increment,
  log_filename varchar(128) default '',
  log_size int(10) default 0,
  log_ip varchar(24) default '',
  log_date timestamp,
  PRIMARY KEY  (log_id),
 KEY (log_filename)
);*/



####################################################################
###  END OF SETTINGS.   DO NOT CHANGE BELOW
####################################################################

// Allow script to work long enough to upload big files (in seconds, 2 days by default)
@set_time_limit(172800);

// following may need to be uncommented in case of problems
// ini_set("session.gc_maxlifetime","10800");

function showUploadForm($message='') {
  $max_file_size_tag = '';
  if (MAX_FILE_SIZE > 0) {
    // convert to bytes
    $max_file_size_tag = "<input name='MAX_FILE_SIZE' value='".(MAX_FILE_SIZE*1024)."'         type='hidden' >\n";
  }

  // Load form template
  include ('index.php');
}

// errors list
$errors = array();

$message = '';

// we should not exceed php.ini max file size
$ini_maxsize = ini_get('upload_max_filesize');
if (!is_numeric($ini_maxsize)) {
  if (strpos($ini_maxsize, 'M') !== false)
    $ini_maxsize = intval($ini_maxsize)*1024*1024;
  elseif (strpos($ini_maxsize, 'K') !== false)
    $ini_maxsize = intval($ini_maxsize)*1024;
  elseif (strpos($ini_maxsize, 'G') !== false)
    $ini_maxsize = intval($ini_maxsize)*1024*1024*1024;
}
if ($ini_maxsize < MAX_FILE_SIZE*1024) {
  $errors[] = "Alert! Maximum upload file size in php.ini (upload_max_filesize) is less         than script's MAX_FILE_SIZE";
}

// show upload form
if (!isset($_POST['submit'])) {
  showUploadForm(join('',$errors));
}

// process file upload
else {

  while(true) {

    // make sure destination folder exists
    if (!@file_exists(DESTINATION_FOLDER)) {
      $errors[] = "Destination folder does not exist or no permissions to see it.";
      break;
    }

    // check for upload errors
    $error_code = $_FILES['filename']['error'];
    if ($error_code != UPLOAD_ERR_OK) {
      switch($error_code) {
        case UPLOAD_ERR_INI_SIZE: 
          // uploaded file exceeds the upload_max_filesize directive in php.ini
          $errors[] = "File is too big (1).";
          break;
        case UPLOAD_ERR_FORM_SIZE: 
          // uploaded file exceeds the MAX_FILE_SIZE directive that was specified in         the HTML form
          $errors[] = "File is too big (2).";
          break;
         case UPLOAD_ERR_PARTIAL:
          // uploaded file was only partially uploaded.
          $errors[] = "Could not upload file (1).";
          break;
        case UPLOAD_ERR_NO_FILE:
          // No file was uploaded
          $errors[] = "Could not upload file (2).";
          break;
        case UPLOAD_ERR_NO_TMP_DIR:
          // Missing a temporary folder
          $errors[] = "Could not upload file (3).";
          break;
        case UPLOAD_ERR_CANT_WRITE:
          // Failed to write file to disk
          $errors[] = "Could not upload file (4).";
          break;
        case 8:
          // File upload stopped by extension
          $errors[] = "Could not upload file (5).";
          break;
      } // switch

      // leave the while loop
      break;
    }

    // get file name (not including path)
    $filename = @basename($_FILES['filename']['name']);

    // filename of temp uploaded file
    $tmp_filename = $_FILES['filename']['tmp_name'];

    $file_ext = @strtolower(@strrchr($filename,"."));
    if (@strpos($file_ext,'.') === false) { // no dot? strange
      $errors[] = "Suspicious file name or could not determine file extension.";
      break;
    }
    $file_ext = @substr($file_ext, 1); // remove dot

    // check file type if needed
    if (count($exts)) {   /// some day maybe check also $_FILES['user_file']['type']
      if (!@in_array($file_ext, $exts)) {
        $errors[] = "Files of this type are not allowed for upload.";
        break;
      }
    }

    // destination filename, rename if set to
    $dest_filename = $filename;
    if (RENAME_FILE) {
      $dest_filename = md5(uniqid(rand(), true)) . '.' . $file_ext;
    }
    // append predefined string for safety
    $dest_filename = $dest_filename . APPEND_STRING;

    // get size
    $filesize = intval($_FILES["filename"]["size"]); // filesize($tmp_filename);

    // make sure file size is ok
   if (MAX_FILE_SIZE > 0 && MAX_FILE_SIZE*1024 < $filesize) {
      $errors[] = "File is too big (3).";
      break;
    }

    if (!@move_uploaded_file($tmp_filename , DESTINATION_FOLDER . $dest_filename)) {
      $errors[] = "Could not upload file (6).";
      break;
    }

    if (DO_LOG) {
      // Establish DB connection
      $link = @mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
      if (!$link) {
        $errors[] = "Could not connect to mysql.";
        break;
      }
      $res = @mysql_select_db(DB_DATABASE, $link);
      if (!$res) {
        $errors[] = "Could not select database.";
        break;
      }
      /*$m_ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
     $m_size = $filesize;
      $m_fname = mysql_real_escape_string($dest_filename);
      $sql = "insert into _uploads_log (log_filename,log_size,log_ip) values         ('$m_fname','$m_size','$m_ip')";
      $res = @mysql_query($sql);
      if (!$res) {
        $errors[] = "Could not run query.";
        break;
      }*/
      @mysql_free_result($res);
      @mysql_close($link);
    } // if (DO_LOG)


   // redirect to upload success url
    header('Location: ' . SUCCESS_URL);
    die();

    break;

  } // while(true)

  // Errors. Show upload form.
  $message = join('',$errors);
  showUploadForm($message);

}

?>
//要将文件上载到的文件夹。必须以斜杠结尾/
定义('DESTINATION_FOLDER','../uploads/');
//允许的最大文件大小,Kb
//设置为零以允许任何大小
定义(“最大文件大小”,10240);
//上传成功网址。上传后,用户将被重定向到此页面。
定义('SUCCESS_URL','myinfo');
//允许的文件扩展名。将仅在不为空时允许这些扩展。
//示例:$exts=array('avi','mov','doc');
$exts=array('jpg','jpeg','png','gif');
//上传后重命名文件?false-保留原始文件名,true-重命名为某个唯一的文件名
定义('RENAME_FILE',true);
//将一个字符串附加到上传的文件名(扩展名后);
//这将通过上传潜在的不安全文件降低被黑客攻击的风险;
//示例字符串:aaa、my等。
定义('APPEND_STRING','');
//需要上传日志吗?日志将保存在MySql数据库中。
定义('DO_LOG',true);
//MySql数据(如果您想保存上传日志)
定义('DB_HOST','my info');//主机,通常是本地主机
定义('DB_数据库','my info');//数据库名称
定义('DB_USERNAME','my info');//用户名
定义('DB_密码','my info');//密码
/*创建表上载日志(
log_id int(11)无符号非空自动增量,
日志文件名varchar(128)默认值“”,
日志大小int(10)默认为0,
log_ip varchar(24)默认值“”,
日志日期时间戳,
主键(日志id),
密钥(日志文件名)
);*/
####################################################################
###设置结束。不要在下面更改
####################################################################
//允许脚本工作足够长的时间以上载大文件(秒,默认为2天)
@设置时间限制(172800);
//如果出现问题,可能需要取消注释以下内容
//ini_集合(“session.gc_maxlifetime”,“10800”);
函数showUploadForm($message=''){
$max_file_size_tag='';
如果(最大文件大小>0){
//转换为字节
$max\u file\u size\u tag=“\n”;
}
//加载表单模板
包括('index.php');
}
//错误列表
$errors=array();
$message='';
//我们不应该超过php.ini的最大文件大小
$ini_maxsize=ini_get('upload_max_filesize');
如果(!是数值($ini\U maxsize)){
if(strpos($ini_maxsize,'M')!==false)
$ini\U maxsize=intval($ini\U maxsize)*1024*1024;
elseif(strpos($ini_maxsize,'K')!==false)
$ini\U maxsize=intval($ini\U maxsize)*1024;
elseif(strpos($ini_maxsize,'G')!==false)
$ini\U maxsize=intval($ini\U maxsize)*1024*1024*1024;
}
如果($ini\u maxsize0&&MAX\u文件大小*1024<$filesize){
$errors[]=“文件太大(3)。”;
打破
}
如果(!@move\u上传的文件($tmp\u文件名,目标文件夹。$dest\u文件名)){
$errors[]=“无法上载文件(6)。”;
打破
}
如果(做日志){
//建立数据库连接
$link=@mysql\u connect(DB\u主机、DB\u用户名、DB\u密码);
如果(!$link){
<form method="post" enctype="multipart/form-data" action="file-upload.php">
    <div><?php echo $message; ?></div><?php echo $max_file_size_tag; ?>
        Please select file to upload:     <input multiple type="file" size="20" name="filename">
     <input type="submit" multiple value="Upload" name="submit">
     <label style="display:block;position:absolute;left:-9999px">
    Are you a bot?
<input type=checkbox name=honeypot value=1>
    </label>
</form>