文件上传不检查现有的文件 [英] File upload is not checking for the existing file

查看:214
本文介绍了文件上传不检查现有的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件上传程序。我想添加一个功能的程序,这是检查目录中是否存在相同的文件。



我想像这样添加到我的代码

  if(file_exists(directory_name /。$ _FILES [file] [name ]))
{
echo $ _FILES [file] [name]。 已经存在。 ;
}

这里是我的php文件上传器代码:

 <?php 

$ valid_formats = array(jpg,png, gif,JPEG,bmp,JPG,PNG,GIF);
$ max_file_size = 1024 * 500; // 100 kb
$ path =images / $ _ POST [fol] /; //上传目录
$ count = 0;
$ b $ if(isset($ _ POST)and $ _SERVER ['REQUEST_METHOD'] ==POST){
//循环$ _FILES来执行所有文件
foreach($ _FILES ['files'] ['name'] as $ f => $ name){
if($ _FILES ['files'] ['error'] [$ f] == 4){
继续; ($ _FILES ['files'] ['error'] [$ f] == 0){

if($ _FILES ['files'] [ 'size'] [$ f]> $ max_file_size){
$ message [] =$ name is too large !.;
继续;

elseif(!in_array(pathinfo($ name,PATHINFO_EXTENSION),$ valid_formats)){
$ message [] =$ name不是有效的格式;
继续;


$ b其他{
if(move_uploaded_file($ _ FILES [files] [tmp_name] [$ f],$ path。$ name) )
$ count ++;
}
}
}
}
?>

如何结合两者?任何人都可以帮忙吗?

解决方案

在移动文件之前检查文件是否存在。
尝试像


  else {
if(file_exists($ path。$ name)
{
file already exixts

if(move_uploaded_file($ _ FILES [files] [tmp_name] [$ f],$ path。$ name))
$ count ++;
}


I have a php file uploader program. I want to add one more functionality to the program, that is checking whether the same file exists in the directory.

I want to add something like this to my code:

if (file_exists("directory_name/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }  

And Here is my php file uploader code:

<?php

$valid_formats = array("jpg", "png", "gif", "JPEG", "bmp", "JPG", "PNG", "GIF");
$max_file_size = 1024*500; //100 kb
$path = "images/$_POST[fol]/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; 
        }          
        if ($_FILES['files']['error'][$f] == 0) {              

            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue;
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; 
            }


            else{ 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; 
            }
        }
    }
}
?>

How to combine both? can anyone help?

解决方案

Check before moving the file whether the file exists or not .. Try something like

        else{ 
                if (file_exists($path.$name)
                {
                  file already exixts
                }  
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; 
            }

这篇关于文件上传不检查现有的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆