首页 > 解决方案 > 将图像存储在数据库或文件管理器之外的某个地方,我可以在其中全局访问它

问题描述

我有一个 6 页的多步骤表格。每页有 2-3 个输入。每页都有下一个和上一个按钮。每当用户在其中一个页面中填写数据并提交时,都会通过 ajax 调用调用一个 php 文件。所有步骤的验证都在同一个文件中完成。如果所有输入都有正确的数据,则显示下一页。所有步骤都由它们的步骤编号定义。

在最后第二步,有一个文件输入,它获取图像。通过 ajax 发送并存储在文件中。我必须将它存储在某个地方的文件中才能显示给用户。

现在在最后一步,必须显示所有数据以进行最后验证。用户输入的所有内容都会显示给他。他选择关闭所有数据或将其存储在配置文件中的位置。

如果他什么都不做并关闭标签怎么办。会话变量会被破坏,但他上传的图像文件将永远卡在文件中。

所以,我的问题是我如何以这种方式存储该图像,以便用户除了(关闭或接受)之外的任何活动,图像都会从文件中删除。

一个疯狂的猜测:我可以将它存储在会话中吗?还是饼干?

** 这是上传验证的代码 **

/* Page 5 */
if($page == $page5)
{
    if(isset($_FILES['uploadedVisitingCard']))
    {
        $image = new image;
        $outImage;

        try{
            if(!list($width, $height, $type) = getimagesize($_FILES['uploadedVisitingCard']['tmp_name']))
            {
                throw new Exception("Either Uploaded File Is Not An Image or It An Image Type That Is Not Supported");
            }
            $image->type = $type;
            $image->width = $width;
            $image->height = $height;
        }
        catch(Exception $e)
        {
            $result['error'] = true;
            $result['msg'] = $e->getMessage();
            echo json_encode($result);
            die();
        }

        if(!in_array($image->type, $allowedImageType))
        {
            $result['error'] = true;
            $result['msg'] = "File Type Not Supported For Uploading! The File Must Be a PNG File or a JPG File";
            echo json_encode($result);
            die();              
        }

        try{
            if(!$image->size = filesize($_FILES['uploadedVisitingCard']['tmp_name']))
            {
                throw new Exception("Unable To Get The Size OF The Uploaded Image.");
            }
        }
        catch(Exception $e){
            $result['error'] = true;
            $result['msg'] = $e->getMessage();
            echo json_encode($result);
            die();
        }

        if($image->size > 5242880)
        {
            $result['error'] = true;
            $result['msg'] = "The Image Must Be Below 5MB";
            echo json_encode($result);
            die();
        }

        try{
            switch ($image->type)
            {
                case IMAGETYPE_JPEG:
                    if(!$outImage = imagecreatefromjpeg($_FILES['uploadedVisitingCard']['tmp_name']))
                    {
                        throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEG");
                    }

                    if(!$outImage = resize_image($outImage, $image, 640, 480))
                    {
                        throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEGFUNC");
                    }

                    if(!imagejpeg($outImage, "main.jpg", 70))
                    {
                        throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVJPEG");
                    }

                    $result['error'] = true;
                    $result['msg'] = "Image Is JPEG";
                    echo json_encode($result);
                    die();
                    break;

                case IMAGETYPE_PNG:
                    if(!$outImage = imagecreatefrompng($_FILES['uploadedVisitingCard']['tmp_name']))
                    {
                        throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNG");
                    }

                    if(!$outImage = resize_image($outImage, $image, 800, 600))
                    {
                        throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNGFUNC");
                    }

                    if(!imagepng($outImage, "main.png", 9))
                    {
                        throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVPNG");
                    }

                    $result['error'] = true;
                    $result['msg'] = "Image Is PNG";
                    echo json_encode($result);
                    die();
                    break;
            }
        }
        catch(Exception $e)
        {
            $result['error'] = true;
            $result['msg'] = $e->getMessage();
            echo json_encode($result);
            die();
        }

        $result['error'] = true;
        $result['msg'] = "Width = ".$image->width." \nHeight = ".$image->height." \nImage Type = ".$image->type;
        echo json_encode($result);
        die();
    }
    else
    {
        $result['error'] = true;
        $result['msg'] = "The Image Was NOt Uploaded";
        echo json_encode($result);
        die();
    }
}

** 这是获取图像细节、调整它们大小并在文件中渲染图像的函数**

function resize_image($file, image $image, $dstWidth, $dstHeight) 
{
    $dst; $newwidth; $newheight;
    $r = $image->width / $image->height;
    if ($dstWidth/$dstHeight > $r) { $newwidth = $dstHeight*$r; $newheight = $dstHeight; }
    else { $newheight = $dstWidth/$r; $newwidth = $dstWidth; }

    switch($image->type)
    {
        case IMAGETYPE_JPEG:
            try{
                if(!$dst = imagecreatetruecolor($newwidth, $newheight)){
                    throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPJPEGFUNC1");
                }
                if(!imagecopyresampled($dst, $file, 0, 0, 0, 0, $newwidth, $newheight, $image->width, $image->height)){
                    throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPJPEGFUNC2");
                }
            }
            catch(Execption $e)
            {
                $result['error'] = true;
                $result['msg'] = $e->getMessage();
                echo json_encode($result);
                die();
            }
            break;
        case IMAGETYPE_PNG:
            try{
                if(!$dst = imagecreatetruecolor($newwidth, $newheight)){
                    throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPPNGFUNC1");
                }
                if(!imagecopyresampled($dst, $file, 0, 0, 0, 0, $newwidth, $newheight, $image->width, $image->height)){
                    throw new Exception("Problem In Duplication Of The File! Error Code: IMGDUPPNGFUNC2");
                }
            }
            catch(Execption $e)
            {
                $result['error'] = true;
                $result['msg'] = $e->getMessage();
                echo json_encode($result);
                die();
            }
            break;
    }
    return $dst;
}


function process_image($file)
{
    global $allowedImageType;
    $image = new Image;

    try{
        if(!list($width, $height, $type) = getimagesize($file))
        {
            throw new Exception("Either Uploaded File Is Not An Image or It An Image Type That Is Not Supported");
        }

        $image->type = $type;
        $image->width = $width;
        $image->height = $height;
    }
    catch(Exception $e)
    {
        $result['error'] = true;
        $result['msg'] = $e->getMessage();
        echo json_encode($result);
        die();
    }

    if(!in_array($image->type, $allowedImageType))
    {
        $result['error'] = true;
        $result['msg'] = "File Type Not Supported For Uploading! The File Must Be a PNG File or a JPG File";
        echo json_encode($result);
        die();              
    }

    try{
        if(!$image->size = filesize($file))
        {
            throw new Exception("Unable To Get The Size OF The Uploaded Image.");
        }
    }
    catch(Exception $e){
        $result['error'] = true;
        $result['msg'] = $e->getMessage();
        echo json_encode($result);
        die();
    }

    if($image->size > 5242880)
    {
        $result['error'] = true;
        $result['msg'] = "The Images Must Be Below 5MB";
        echo json_encode($result);
        die();
    }

    return $image;
}

function render_image($image, $path)
{
    try{
        switch ($image->type)
        {
            case IMAGETYPE_JPEG:
                if(!$outImage = imagecreatefromjpeg($_FILES['uploadedVisitingCard']['tmp_name']))
                {
                    throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEG");
                }

                if(!$outImage = resize_image($outImage, $image, 640, 480))
                {
                    throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPJPEGFUNC");
                }

                if(!imagejpeg($outImage, "main.jpg", 70))
                {
                    throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVJPEG");
                }

                $result['error'] = true;
                $result['msg'] = "Image Is JPEG";
                echo json_encode($result);
                die();
                break;

            case IMAGETYPE_PNG:
                if(!$outImage = imagecreatefrompng($_FILES['uploadedVisitingCard']['tmp_name']))
                {
                    throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNG");
                }

                if(!$outImage = resize_image($outImage, $image, 800, 600))
                {
                    throw new Exception("Cannot Reproduce The JPEG Image! Error Code: IMGDUPPNGFUNC");
                }

                if(!imagepng($outImage, "main.png", 9))
                {
                    throw new Exception("Cannot Save The File Into The Servers! Error Code: IMGSAVPNG");
                }

                $result['error'] = true;
                $result['msg'] = "Image Is PNG";
                echo json_encode($result);
                die();
                break;
        }
    }
    catch(Exception $e)
    {
        $result['error'] = true;
        $result['msg'] = $e->getMessage();
        echo json_encode($result);
        die();
    }
}

标签: phpformsmulti-step

解决方案


最简单的方法是创建一个实际的文件管理系统。这意味着,您在某处有一个数据库表,其中存储有关文件的信息。实际文件应存储在磁盘上某处的文件夹结构中,名称随机生成。UUID 对此非常有用。然后在数据库中存储该 UUID/文件路径,以及有关所需文件的任何其他信息;例如,其原始用户提供的名称、上传时间、上传者等。

有了这些信息,您就可以对其采取行动。例如,您可以查询所有已上传超过 24 小时,但在此之后没有在其他任何地方使用过的文件,因此可以将其删除。或者您temporary = true在上传文件时设置了一个标志,在向导结束时您删除了该temporary标志,并temporary在 24 小时后删除了所有文件。这一切都可以通过一个简单的常规 cron 作业来完成。

换句话说:您不会将这些文件与任何其他上传的文件区别对待,您只需在某处保留足够的关于它们的信息,以便您以后在必要时删除它们。


推荐阅读