警告:file_exists()期望参数1为有效路径,在C:\中给出数组

时间:2018-10-23 12:21:33

标签: php html

由于某种奇怪的原因,我收到此警告... 这是完整的警告:

Warning: file_exists() expects parameter 1 to be a valid path, array given in C:\wamp64\www\Petrogas NCR applicatie\Insert.php on line 84

这是我的代码:

for($i = 0; $i < count($_FILES['attachments']['name']); $i++)
    {
        function resize_image($file,$max_resolution) {
            if (file_exists($file)) {

                $original_image = imagecreatefromjpeg($file);

                $original_width = imagesx($original_image);
                $original_height = imagesy($original_image);

                $ratio = $max_resolution / $original_width;
                $new_width = $max_resolution;
                $new_height = $original_height * $ratio;

                if ($new_height > $max_resolution) {
                    $ratio = $max_resolution / $original_height;
                    $new_height = $max_resolution;
                    $new_width = $original_width * $ratio;
                }

                if ($original_image) {
                    $new_image = imagecreatetruecolor($new_width, $new_height);
                    imagecopyresampled($new_image,$original_image,0,0,0,0,$new_width,$new_height,$original_width,$original_height);
                    imagejpeg($new_image,$file,90);
                }
            }
        }
        $filetmp = $_FILES["attachments"]["tmp_name"][$i];
        $filename = $_FILES["attachments"]["name"][$i];
        $filepath = "images/".$filename;
        if (move_uploaded_file($filetmp,$filepath)) {
            $file = $_FILES['attachments']['name'];
            resize_image($file, '50');
            $querypictureupload = "INSERT INTO file_upload(ncrnummer,filename) VALUES ('$ncrnummer','$filename')";
            $result = mysqli_query($conn, $querypictureupload);
        }
    }

我正在尝试上传图片并调整其大小。但是得到了这个奇怪的警告...

1 个答案:

答案 0 :(得分:0)

更改脚本,因为您不应该在for循环内创建函数

for($i = 0; $i < count($_FILES['attachments']['name']); $i++)
{        
    $filetmp = $_FILES["attachments"]["tmp_name"][$i];
    $filename = $_FILES["attachments"]["name"][$i];
    $filepath = "images/".$filename;
    if (move_uploaded_file($filetmp,$filepath)) {
        $file = $_FILES['attachments']['name'];
        resize_image($file, '50', $filepath);
        $querypictureupload = "INSERT INTO file_upload(ncrnummer,filename) VALUES ('$ncrnummer','$filename')";
        $result = mysqli_query($conn, $querypictureupload);
    }
}

在for循环函数范围之外创建函数

您需要在file_exists函数中传递文件路径,以检查文件是否存在

 function resize_image($file,$max_resolution, $filepath) {
        if (file_exists($filepath)) {

            $original_image = imagecreatefromjpeg($file);

            $original_width = imagesx($original_image);
            $original_height = imagesy($original_image);

            $ratio = $max_resolution / $original_width;
            $new_width = $max_resolution;
            $new_height = $original_height * $ratio;

            if ($new_height > $max_resolution) {
                $ratio = $max_resolution / $original_height;
                $new_height = $max_resolution;
                $new_width = $original_width * $ratio;
            }

            if ($original_image) {
                $new_image = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($new_image,$original_image,0,0,0,0,$new_width,$new_height,$original_width,$original_height);
                imagejpeg($new_image,$file,90);
            }
        }
    }