0

希望有人可以在这里帮助我。整夜浏览,我尝试的任何东西似乎都不起作用,但我是 php 新手,所以我很慢。我需要上传 6 张图片,这很好用。但后来我意识到您不仅可以上传图像,还可以上传所有其他文件类型。我试图能够将其限制为每个 100kb 以下的图像。嘻嘻嘻嘻嘻嘻嘻!!!请!

function findexts ($filename) { $filename = strtolower('$filename') ; 
$exts = preg_split("[/\\.]", $filename) ;
 $n = count($exts)-1; 
 $exts = $exts[$n];
  return $exts;
   }

$ext = findexts ($_FILES['images']['name']) ; 
$ran = rand ();
$ran2 = $ran.".";

while(list($key,$value) = each($_FILES['images']['name']))
        {
            if(!empty($value))
            {
                $filename = $ran.$value;
                    $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                    $add = "media/".$ran."$filename";
                $insert_query = "INSERT INTO ....VALUES ...";
                       //echo $_FILES['images']['type'][$key];
                 // echo "<br>";
                    copy($_FILES['images']['tmp_name'][$key], $add);
                    chmod("$add",0777);

        mysql_query($insert_query);

            }

        }
4

3 回答 3

3

在此处查看您的两个问题的答案:

https://stackoverflow.com/a/9153419/723855

将此函数添加到您的脚本(从链接修改):

function acceptFileUpload($thefile){
    if(isset($_FILES[$thefile])) {
        $errors     = array();
        $maxsize    = 2097152;
        $acceptable = array(
            'application/pdf',
            'image/jpeg',
            'image/jpg',
            'image/gif',
            'image/png'
        );

        if(($_FILES[$thefile]['size'] >= $maxsize) || ($_FILES[$thefile]["size"] == 0)) {
            $errors[] = 'File too large. File must be less than 2 megabytes.';
        }

        if(!in_array($_FILES[$thefile]['type'], $acceptable)) && (!empty($_FILES[$thefile]["type"]))) {
            $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
        }

        if(count($errors) !== 0) {
            return true;
        } else {
            foreach($errors as $error) {
                echo '<script>alert("'.$error.'");</script>';
                return false;
            }

            die(); //Ensure no more processing is done
        }
    }
}

然后在您的脚本中更改您的 while 循环以使用此函数来检查有效文件:

while(list($key,$value) = each($_FILES['images']['name']))
    {
        if(!empty($value))
        {
            if(acceptFileUpload('images'))
            {
            $filename = $ran.$value;
                $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                $add = "media/".$ran."$filename";
            $insert_query = "INSERT INTO ....VALUES ...";
                   //echo $_FILES['images']['type'][$key];
             // echo "<br>";
                copy($_FILES['images']['tmp_name'][$key], $add);
                chmod("$add",0777);

    mysql_query($insert_query);
            }
        }

    }

我可能没有传递给acceptFileUpload()的那个参数权。

于 2012-04-16T13:44:40.393 回答
0

你可以检查文件类型

$_FILES['image']['type']

或者如果您也想检查扩展名

$extension = explode('.',(string)$_FILES['image']['name']);
//then check if its "jpg", "gif" or "png"

文件大小可以检查

$_FILES['image']['size']

因此,对于每个图像更新,您的脚本应该是这样的:

$extension = explode('.',$_FILES['image']['name']);
$imgextensions = array();
$size      = $_FILES['image']['size'];
if(($extension == 'jpg' || $extension == 'gif' || $extension == 'png') &&
    $size < 100000 ){
    // upload your file to your filesystem
}else{
    //inform the user
}
于 2012-04-16T13:43:08.083 回答
0

在每个文件的处理脚本上运行四个函数,如果所有测试都通过则文件满足您的条件并且可以安全存储(png / jpg / gif + 非零 + 10Kb 限制 + 上传文件)

//Example Call: checkFileExtension($_FILES['fieldname']['name']);
function checkFileExtension($filename) {
    $filename = strtolower($filename) ;
    $filenamePartsArray = preg_split("[/\\.]", $filename) ;
    $extension = $filenamePartsArray[count($filenamePartsArray) - 1];
    if (($extension == 'gif') || ($extension == 'jpeg') || ($extension == 'jpg') || ($extension == 'png')) {
        return true;
    } else {
        return false;
    }
}

//Example Call: checkFileMIME($_FILES['fieldname']['type']);
function checkFileMIME($filetype) {
    if (($filetype == 'image/png') || ($filetype == 'image/jpeg') || ($filetype == 'image/gif')) {
        return true;
    } else {
        return false;
    }
}

//Example Call: checkFileSize($_FILES['fieldname']['size'], 10);
function checkFileSize($filesize, $limitKb = 0) {
    if ($filesize == 0) {
        return false;
    }
    if ($limitKb != 0) {
        if ($filesize > ($limitKb * 1024)) {
            return false;
        }
    }
    return true;
}

//Native Call: is_uploaded_file($_FILES['fieldname']['tmp_name']);

编辑:伪示例使用

foreach ($_FILES as $fieldname => $file) {
    if ((checkFileExtension($file['name'])) && (checkFileMIME($file['type'])) && (checkFileSize($file['size'], 10)) && (is_uploaded_file($file['tmp_name']))) {
        //Move the image with move_uploaded_file
        //Save the file location with DB insert
    }
}
于 2012-04-16T13:58:53.180 回答