codeigniter 多图像上传编码器

u7up0aaq  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(154)

我创建图像上传代码。这首先创建一个目录,然后上传图像到它。
在表格中,我上传3张图片。
我的代码是:

$dir_path = 'assets/images/product_images/'.$model;
mkdir($dir_path, 0777);

$i = 1 ;

    foreach ($image as $new_image)
        {
            $dir_path_up = 'assets/images/product_images/'.$model."/";
            $filename = $_FILES["$new_image"]["name"];
            $image_name  = $dir_path_up .$filename . $i. ".jpg";
                echo $image_name;

                $i++;
        }
        die();

超声心动图结果

assets/images/product_images/255_2555/1.jpg
assets/images/product_images/255_2555/2.jpg
assets/images/product_images/255_2555/3.jpg

但是图像没有被上传到目录中。我回显了我创建的图像名称。它已经被重命名了。那么为什么图像没有被上传到目录中呢?
这有什么不对的?

nvbavucw

nvbavucw1#

从此处阅读手册CI image upload
在codignator中我们使用上传库来上传图片。
你根据你的要求传递你的参数

$config['upload_path'] = 'assets/images/product_images/'.$model."/";
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('Your View', $error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $this->load->view('Your View', $data);
                }
eiee3dmh

eiee3dmh2#

当你用PHP上传一个文件时,它会被保存在一个临时文件夹中。你可以用$_ FILES在你的脚本中访问这个文件,但是它仍然在你的临时文件夹中,下次请求时它会被删除。
要保留上传的文件,您需要将其移动到所需位置。
用于此操作的函数称为move_uploaded_file(API:http://php.net/manual/en/function.move-uploaded-file.php

bool move_uploaded_file ( string $filename , string $destination )

在您的情况下,这将导致类似于以下内容的结果:

$dir_path = 'assets/images/product_images/'.$model;
mkdir($dir_path, 0777);

$i = 1 ;

    foreach ($image as $new_image)
        {
            $dir_path_up = 'assets/images/product_images/'.$model."/";
            $filename = $_FILES["$new_image"]["name"];
            $tmp_name = $_FILES["$new_image"]["tmp_name"]
            $image_name  = $dir_path_up .$filename . $i. ".jpg";
            move_uploaded_file($tmp_name, $image_name);
                echo $image_name;

                $i++;
        }
        die();
jhiyze9q

jhiyze9q3#

此功能适用于多个图像上传

<?php
    $j = 0;     // Variable for indexing uploaded image.
    $target_path = 'assets/images/product_images/' . $last_id . '/';     // Declaring Path for uploaded images.
    for ( $i = 0; $i < count($_FILES['image']['name']); $i++ )
    {
        // Loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
        $ext = explode('.', basename($_FILES['image']['name'][$i]));   // Explode file name from dot(.)
        $file_extension = end($ext); // Store extensions in the variable.
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
        $j = $j + 1;      // Increment the number of uploaded images according to the files in array.
        if ( ($_FILES["image"]["size"][$i] < 100000000)     // Approx. 100kb files can be uploaded.
            && in_array($file_extension, $validextensions)
        )
        {
            move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_path);
        }
        else
        {

        }
    }

更新,您可以使用

foreach ($image as $new_image)
{
    $dir_path_up = 'assets/images/product_images/'.$model."/";
    $filename = $_FILES["$new_image"]["name"];
    $image_name  = $dir_path_up .$filename . $i. ".jpg";
    if (move_uploaded_file($_FILES["$new_image"]["tmp_name"], $image_name))
    {
        echo "The file has been uploaded.";
    }
    else
    {
        echo "There was an error uploading the file.";
    }
    $i++;
}

相关问题