微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 多图像上传文件上传错误数量

我想借助数组将一些图像上传到目录.因此我有这个代码

$allowedExtensions = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
            $maxSize = 2097152;
            $Dir = "a/b/c/d";
            $storageDir = "a/b/c/d/tmp_images";

            //Arrays
            $errors2 = $output = array();


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

            // Validation loop (I prefer for loops for this specific task)
            for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {

                $fileName = $_FILES['image']['name'][$i];
                $fileSize = $_FILES['image']['size'][$i];
                /*$fileErr = $_FILES['image']['error'][$i];*/
                $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

                // Dateiendung überprüfen
                if (!in_array($fileExt, $allowedExtensions)) {
                    $errors2[$fileName][] = "format $fileExt in $fileName is not accepted";
                }

                // check filesize
                if ($fileSize > $maxSize) {
                    $errors2[$fileName][] = "maxsize of 2MB exceeded";
                }


            }

            /*// Handle validation errors here
            if (count($errors) > 0) {
                echo "Fehler beim Upload des Bildmaterials:"; 
                echo ($errors); ->gibt "Array" aus
            }*/


            if (is_dir($Dir)){
                mkdir($storageDir, 0755);
            }else{
                mkdir($Dir, 0755);
                mkdir($storageDir, 0755);
            }


            // Fileupload
            for ($i = 0; isset($_FILES['image']['name'][$i]); $i++) {

            // Get base info
            $fileBase = basename($_FILES['image']['name'][$i]);
            $fileName = pathinfo($fileBase, PATHINFO_FILENAME);
            $fileExt = pathinfo($fileBase, PATHINFO_EXTENSION);
            $fileTmp = $_FILES['image']['tmp_name'][$i];

            // Construct destination path
            $fileDst = $storageDir.'/'.basename($_FILES['image']['name'][$i]);
            for ($j = 0; file_exists($fileDst); $j++) {
                $fileDst = "$storageDir/$fileName-$j.$fileExt";
            }

            // Move the file 

            if (count($errors2) == 0) { 
                if (move_uploaded_file($fileTmp, $fileDst)) {
                                    ...
                                }
                        }

代码的问题如下:如果上传两个或多个具有接受结束的文件,它将回显:

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to access abc2.png in /a/b/xxx.PHP on line xxx

指的是那条线:

if (move_uploaded_file($fileTmp, $fileDst)) {

除第一张照片外,每张照片都会显示此信息.所以我不知道我做错了什么.如果有人可以帮助我,我真的很感激.我真的很感激.非常感谢.

解决方法:

你的代码与我在limiting the checking condition while uploading swf files的回答非常相似

这是你应该如何实现这样的..

完整的脚本

<?PHP
error_reporting ( E_ALL );
$allowedExtensions = array (
        'jpg',
        'jpeg',
        'png',
        'bmp',
        'tiff',
        'gif' 
);
$maxSize = 2097152;
$dirImage = "photos/tmp_images";
$errors = $output = array ();
if (isset ( $_FILES ['image'] )) {
    foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {

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

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

        if (empty ( $fileName ))
            continue;

            // Dateiendung überprüfen
        if (! in_array ( $fileExt, $allowedExtensions )) {
            $errors [$fileName] [] = "format $fileExt in $fileName is not accepted";
        }

        if ($fileSize > $maxSize) {
            $errors [$fileName] [] = "maxsize of 2MB exceeded";
        }

        if (! mkdir_recursive ( $dirImage, 0777 )) {
            $errors [$fileName] [] = "Error  Creating /Writing  Directory $dirImage ";
        }

        // Construct destination path
        $fileDst = $dirImage . DIRECTORY_SEParaTOR . $fileName;
        $filePrifix = basename ( $fileName, "." . $fileExt );
        $i = 0;
        while ( file_exists ( $fileDst ) ) {
            $i ++;
            $fileDst = $dirImage . DIRECTORY_SEParaTOR . $filePrifix . "_" . $i . "." . $fileExt;

        }
        // Move the file

        if (count ( $errors ) == 0) {
            if (move_uploaded_file ( $fileTemp, $fileDst )) {
                // ...

                $output [$fileName] = "OK";
            }
        }

    }
}

function mkdir_recursive($pathname, $mode) {
    is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
    return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
}
if (! empty ( $errors )) {
    echo "<pre>";
    foreach ( $errors as $file => $error ) {
        echo $file, PHP_EOL;
        echo "==============", PHP_EOL;
        foreach ( $error as $line ) {
            echo $line, PHP_EOL;
        }
        echo PHP_EOL;
    }
    echo "</pre>";
}

if (! empty ( $output )) {
    echo "<pre>";
    echo "Uploaded Files", PHP_EOL;
    foreach ( $output as $file => $status ) {
        echo $file, "=", $status, PHP_EOL;
    }

    echo "</pre>";
}
?>


<form method="post" enctype="multipart/form-data">
    <label for="file">Filename 1:</label> <input type="file" name="image[]"
        id="file" /> <br /> <label for="file">Filename 2:</label> <input
        type="file" name="image[]" id="file" /> <br /> <label for="file">Filename
        3:</label> <input type="file" name="image[]" id="file" /> <br /> <input
        type="submit" name="submit" value="Submit" />
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐