move_uploaded_file给出无法打开流的错误:无效的参数

时间:2017-04-17 09:32:58

标签: php

这是我的PHP代码,我发送POST请求以将临时文件移动到服务器位置以进行进一步处理,同时通过各种验证。

<?php
    session_start();
    if(isset($_SESSION['user_id']) AND !empty($_SESSION['user_id']) AND isset($_SESSION['role']) AND !empty($_SESSION['role']) AND $_SESSION['role'] == "shopkeeper")
        {
            if(sizeof($_FILES["img"]["name"]) <= 1 OR sizeof($_FILES["img"]["name"]) == 0)
                {
                    echo "Minimum 2 Images are needed";
                }

            elseif(sizeof($_FILES["img"]["name"]) > 4)
                {
                    echo "Maximum 4 images are allowed";
                }

            elseif(sizeof($_FILES["img"]["name"]) > 4)
                {
                    echo "Maximum 4 images are allowed";
                }

            elseif(sizeof($_FILES["img"]["name"]) >= 2 && sizeof($_FILES["img"]["name"]) <=4)
                {
                    $success = $failed = 0;

                    foreach ($_FILES["img"]["name"] as $array_number => $holding_value)
                        {
                            $value = explode('.', $holding_value);

                            if(strtolower($value[1]) == "jpg" OR strtolower($value[1]) == "jpeg")
                                {
                                    $success ++;
                                }
                            else
                                {
                                    $failed ++;
                                }
                        }

                    if(sizeof($_FILES["img"]["name"]) == $success)
                        {
                            $success = $failed = 0;

                            foreach($_FILES["img"]["size"] as $array_number => $holding_value)
                                {
                                    if($holding_value < 4000000) /*for 4mb*/
                                        {
                                            $success ++;
                                        }
                                    else
                                        {
                                            $failed ++;
                                        }
                                }

                            if(sizeof($_FILES["img"]["name"]) == $success)
                                {
                                    function getTime()
                                        {
                                            $timezone = new DateTimeZone("Asia/Kolkata");
                                            $datetime = new DateTime();
                                            $datetime->setTimezone($timezone);

                                            $year = $datetime->format('Y');
                                            $month = $datetime->format('M');
                                            $day = $datetime->format('D');
                                            $date = $datetime->format('d-m-Y');
                                            $timestamp = $datetime->format('Y-m-d_H:i:s');
                                            return array('year' => $year, 'month' => $month, 'day' => $day, 'date' => $date, 'timestamp' => $timestamp);
                                        }

                                    function create_Check_Path($path)
                                        {
                                            if (is_dir($path)) return true;
                                            $prev_path = substr($path, 0, strrpos($path, '/', -2) + 1 );
                                            $return = create_Check_Path($prev_path);
                                            return ($return && is_writable($prev_path)) ? mkdir($path) : false;
                                        }

                                    /*All the path variables declaration start here*/
                                        $temp = "../temp";
                                        $path1 = $temp."/thumbnail_100x141";
                                        $path2 = $temp."/average_300x424";
                                        $path3 = $temp."/original_compressed_700x990";
                                        // $path4 = $temp."/good_400x565";
                                        // $path5 = $temp."/original";
                                    /*All the path variables declaration end here*/

                                    $check_paths = array($temp, $path1, $path2, $path3/*, $path4, $path5*/);

                                    for($i=0; $i<sizeof($check_paths); $i++)
                                        {
                                            create_Check_Path($check_paths[$i]);
                                        }

                                    require_once('phpthumb/phpthumb.class.php');
                                    $phpThumb = new phpThumb();
                                    $output_format = 'jpg';
                                    $image_heights = array(141, 424, 990);
                                    $image_widths = array(100, 300, 700);
                                    $counter = sizeof($_FILES["img"]["tmp_name"]);
                                    $count = 0;
                                    $values = array();

                                    foreach ($_FILES["img"]["tmp_name"] as $array_number => $holding_value)
                                        {
                                            $date =  getTime();
                                            $tmp_img_name = $_SESSION["username"]."_".$date["timestamp"]."."."jpg";

                                            if(move_uploaded_file($holding_value, $temp."/".$tmp_img_name))
                                                {
                                                    $phpThumb->resetObject();
                                                    $phpThumb->setSourceFilename($temp."/".$tmp_img_name);
                                                    $phpThumb->setParameter('config_output_format', $output_format);

                                                    if($count == 0)
                                                        {
                                                            $phpThumb->setParameter('h', $image_heights[$count]);
                                                            $phpThumb->setParameter('w', $image_widths[$count]);
                                                            $phpThumb->setParameter('q', 90);
                                                            $store_filename = $path1."_".$tmp_img_name;
                                                        }
                                                    if($count == 1)
                                                        {
                                                            $phpThumb->setParameter('h', $image_heights[$count]);
                                                            $phpThumb->setParameter('w', $image_widths[$count]);
                                                            $phpThumb->setParameter('q', 90);
                                                            $store_filename = $path2."_".$tmp_img_name;
                                                        }
                                                    if($count == 2)
                                                        {
                                                            $phpThumb->setParameter('h', $image_heights[$count]);
                                                            $phpThumb->setParameter('w', $image_widths[$count]);
                                                            $phpThumb->setParameter('q', 80);
                                                            $store_filename = $path3."_".$tmp_img_name;
                                                        }
                                                    if($phpThumb->GenerateThumbnail())
                                                        {
                                                            if($phpThumb->RenderToFile($store_filename))
                                                                {
                                                                    $values[] = $store_filename;
                                                                    // include '../../src/Cloudinary.php';
                                                                    // include '../../src/Uploader.php';

                                                                    // \Cloudinary::config(array
                                                                    //  (
                                                                    //      "cloud_name" => "dczebgs6w",
                                                                    //      "api_key" => "328212291514632",
                                                                    //      "api_secret" => "LvhRA-s59TZ30rPhCieTVx8LzIw"
                                                                    //  ));

                                                                    // $cloudUpload = \Cloudinary\Uploader::upload($store_filename, array("timeout" => 120));

                                                                    // if($cloudUpload)
                                                                }
                                                            else
                                                                {
                                                                    //unable to write file to final destination directory - check folder permissions
                                                                    $message= "Error! Please try again (render).";
                                                                }
                                                        }
                                                    else
                                                        {
                                                            $message= "Error! Please try again (generate).";
                                                        }
                                                }

                                            $count++;
                                            unlink($temp."/".$tmp_img_name);
                                        }

                                    print_r($values);
                                }
                            else
                                {
                                    if($failed == 1)
                                        {
                                            echo $failed." image you are trying to upload is greater than 4Mb. Please upload an image less than 4Mb";
                                        }
                                    else
                                        {
                                            echo "1 or more images you are trying to upload is greater than 4Mb. Please upload an image less than 4Mb. Total images with size greater than 4Mb are: ( ".$failed." )";
                                        }   
                                }
                        }
                    else
                        {
                            if($failed == 1)
                                {
                                    echo $failed." image you are trying to upload is not a valid format. Please upload a image with .jpg format only";
                                }
                            else
                                {
                                    echo "1 or more images you are trying to upload are not valid. Please upload a image with .jpg format only. Total invalid files found: ( ".$failed." )";
                                }
                        }   
                }

            else
                {
                    echo "there was error validating images. Please try again";
                }
        }
    else
        {
            header("Location: logout.php");
        }   
?>

代码的问题是它给出了错误“

  

警告:   move_uploaded_file(../温度/ simmifashion_2017-04-17_14:58:31.jpg):   无法打开流:此部分的第108行上的参数无效   line =&gt;如果(move_uploaded_file($ holding_value,   $温度。 “/”。$ tmp_img_name))

我无法理解我的逻辑失败的地方。

如何更改或更新我的代码以获得理想的结果?

0 个答案:

没有答案