0

我使用脚本将几张图片上传到一个目录,如果只上传一张图片,代码效果很好。

如果我想上传两张或更多张图片并且有一个不被接受的扩展名,脚本将上传一张带有允许上传的扩展名的图片,并为不被接受的一张显示错误消息,但上传发生了. 这是我的第一个问题。

第二个问题:如果出现错误消息,我想显示一条消息,其中说,哪些图像是不允许的。我不知道如何将这个具有不被接受的结尾的变量提取到我可以回显到错误消息的变量中。

这是我使用的代码:

<?php
if(!empty($_FILES['image']['tmp_name'])){

    $allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');

    foreach($_FILES['image']['name'] as $key => $array_value){

        $file_name = $_FILES['image']['name'][$key];
        $file_size = $_FILES['image']['size'][$key];
        $file_tmp = $_FILES['image']['tmp_name'][$key];

        $file_extension = strtolower(end(explode('.', $file_name)));
        if (in_array($file_extension, $allowed_extension) === false){
            $errors[] = 'its an unaccepted format in picture $variable_that_count';
            continue;
        }

        if ($file_size > 2097152){
            $errors[] = 'reached maxsize of 2MB per file in picture $variable_that_count';
        }

        if (count($errors) == 0){
            $path = "a/b/c/";
            $uploadfile = $path."/".basename($_FILES['image']['name'][$key]);

            if (move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadfile)){
                echo "success.";
            }
        }
    }
}
?>

希望很清楚我喜欢接近什么。如果有人可以提供帮助,我将不胜感激。非常感谢。

4

1 回答 1

1

这对你有用

示例 HTML

<form action="" enctype="multipart/form-data" method="post">

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <p>
        Please specify a file, or a set of files:<br> <input type="file"
            name="image[]" size="40">
    </p>

    <div>
        <input type="submit" value="Send">
    </div>
</form>

PHP 代码

$allowedExtention = array (
        'jpg',
        'jpeg',
        'png',
        'bmp',
        'tiff',
        'gif' 
);
$errors = array ();
$output = array ();

if (! empty ( $_FILES ['image'] ['tmp_name'] )) {

    foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {

        $fileName = $_FILES ['image'] ['name'] [$key];
        $fileSize = $_FILES ['image'] ['size'] [$key];
        $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];

        $fileExtention = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExtention = strtolower ( $fileExtention );

        if (! in_array ( $fileExtention, $allowedExtention )) {
            $errors [$fileName] [] = "File format $fileExtention not accepted for $fileName";
            continue;
        }

        if ($fileSize > 2097152) {
            $errors [$fileName] [] = 'reached maxsize of 2MB per file in picture $variable_that_count';
continue ;
        }

        if (count ( $errors ) == 0) {
            $path = "temp";
            $prifix = basename ( $fileName, "." . $fileExtention );

            var_dump ( $prifix );

            $uploadfile = $path . "/" . $fileName;
            $x = 0;
            while ( file_exists ( $uploadfile ) ) {
                $x ++;
                $uploadfile = "{$path}/{$prifix}-{$x}.{$fileExtention}";
            }

            if (move_uploaded_file ( $fileTemp, $uploadfile )) {
                $fileName = basename ( $uploadfile );
                $output [$fileName] = "OK";
            } else {
                $output [$fileName] = "ERORR";
                $errors [$fileName] [] = "Can Move uploaded file to destination";
            }
        }
    }
}

var_dump ( $errors );
var_dump ( $output );

样本输出

string '79534296' (length=8)
string '89773706' (length=8)
array
  'download (1)' => 
    array
      0 => string 'File format  not accepted for download (1)' (length=42)
  'brief.docx' => 
    array
      0 => string 'File format docx not accepted for brief.docx' (length=44)
  '' => 
    array
      0 => string 'File format  not accepted for ' (length=30)
array
  '79534296-2.jpg' => string 'OK' (length=2)
  '89773706-2.jpg' => string 'OK' (length=2)

编辑 1

如果所有文件都必须有效,则有两种方法可以实现

A. 首先验证所有文件;

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
    if(! in_array (pathinfo ($_FILES ['image'] ['name'] [$key], PATHINFO_EXTENSION ), $allowedExtention ))
    {
        die("Die! Die! Die") ;
    }
}

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

B. 如果检测到错误,删除所有文件

foreach ( $_FILES ['image'] ['name'] as $key => $array_value ) {
  // Upload Script here 
 }

// Remove All Files
if(count($errors) > 0)
{
    foreach ($output as $key => $value)
    {
        @unlink($path . "/" . $key);
    }

    die("Die! die! die!") ;
}

我希望这有帮助

于 2012-04-10T19:23:16.337 回答