Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i like to upload some images to a directory with the help of arrays. therefore i have this code:

$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)) {
                                    ...
                                }
                        }

the problem with that code is the following: in case of uploading two or more files with an accepted ending it will echo out:

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

which refers to that line:

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

this message will be shown for each picture except the first one. so i have no clue what i do wrong. i would really appreciate if there is someone who could help me out. i really would appreciate. thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

First of, I would name the uploaded fields seperatly. E.g. name the first field <input name="image_1" type="file" /> and the second <input name="image_2" type="file" />. Then you can iterate over the $_FILES array instead:

foreach($_FILES as $fileId => $file){
    //make sure it's a file you want (optional)
    if(!preg_match("/^image\_d+$/",$fileId){
         continue;
    }

    //the rest of your code from the for loop
}

Secondly, you need to make sure that your form's enctype is multipart/form-data.

Does any of this help?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...