php - 链接应在五次访问后过期

标签 php

我能够使用我的文件上传系统生成链接,现在经过五次访问...我需要该链接,如果打开应显示一条已过期的消息。

<?php
function display_upload_form()
{
echo <<<DISPLAY_UPLOAD_FORM

    <html>
    <head>
    <title>Yet Another Upload Form</title>
    <style type="text/css" media="screen">
        <!--
        html body
        {background:#fff; font: 76%/1.5em arial, helvetica, sans-serif; color:#333;}

        input
        {color:#333;}
        -->
    </style>
    </head>

    <body>

    <form method="post" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data">

    <p>Select a file.<br />
    <input type="file" name="myfile" tabindex="1" /></p>

    <p><input type="hidden" name="execute" value="1" /></p>

    <p><input type="submit" value="Upload File" tabindex="2" />

    </form>

    </body>
    </html>

DISPLAY_UPLOAD_FORM;
}


function execute_upload()
{
    // root path
    $path = $_SERVER['DOCUMENT_ROOT'];

    // upload directory. path will originate from root.
    $dirname = '/uploads';

    // permission settings for newly created folders
    $chmod = 0755;

    // create file vars to make things easier to read.
    $filename = $_FILES['myfile']['name'];
    $filesize = $_FILES['myfile']['size'];
    $filetype = $_FILES['myfile']['type'];
    $file_tmp = $_FILES['myfile']['tmp_name'];
    $file_err = $_FILES['myfile']['error'];
    $file_ext = strrchr($filename, '.');

    // check if user actually put something in the file input field.
    if (($file_err == 0) && ($filesize != 0))
    {
        // Check extension.
        if (!$file_ext)
        {
            unlink($file_tmp);
            die('File must have an extension.');
        }

        // extra check to prevent file attacks.
        if (is_uploaded_file($file_tmp))
        {
            /*
            * check if the directory exists
            * if it doesnt exist, make the directory
            */
            $dir = $path . $dirname;

            if (!is_dir($dir))
            {
                $dir = explode('/', $dirname);

                foreach ($dir as $sub_dir)
                {
                    $path .= '/' . $sub_dir;
                    if (!is_dir($path))
                    {
                        if (!mkdir($path, $chmod))
                        {
                            unlink($file_tmp);
                            die('<strong>Error:</strong> Directory does not exist and was unable to be created.');
                        }
                    }
                }
            }

            /*
            * copy the file from the temporary upload directory
            * to its final detination.
            */
            if (@move_uploaded_file($file_tmp, $dir . '/' . $filename))
            {
                // success!
                echo "
                <p>Success!</p>
                <p><strong>View File:</strong> <a href=\"$dirname/$filename\">$filename</a></p>
                ";
            }
            else
            {
                // error moving file. check file permissions.
                unlink($file_tmp);
                echo '<strong>Error:</strong> Unable to move file to designated directory.';
            }
        }
        else
        {
            // file seems suspicious... delete file and error out.
            unlink($file_tmp);
            echo '<strong>Error:</strong> File does not appear to be a valid upload. Could be a file attack.';
        }
    }
    else
    {
        // Kill temp file, if any, and display error.
        if ($file_tmp != '')
        {
            unlink($file_tmp);
        }

        switch ($file_err)
        {
            case '0':
                echo 'That is not a valid file. 0 byte length.';
                break;

            case '1':
                echo 'This file, at ' . $filesize . ' bytes, exceeds the maximum allowed file size as set in <em>php.ini</em>. '.
                'Please contact your system admin.';
                break;

            case '2':
                echo 'This file exceeds the maximum file size specified in your HTML form.';
                break;

            case '3':
                echo 'File was only partially uploaded. This could be the result of your connection '.
                'being dropped in the middle of the upload.';

            case '4':
                echo 'You did not upload anything... Please go back and select a file to upload.';
                break;
        }
    }
}

// Logic Code *****************************************************************

if (isset($_POST['execute']))
{
    execute_upload();
}
else
{
    display_upload_form();
}


?>

我正在使用我得到的以下代码。

最佳答案

您可能必须将计数器存放在某个地方。文本文件,或者最好是数据库。您将从具有文件字段和当前计数字段的单表数据库中受益匪浅。

当请求文件时,您可以检查数据库中的计数。如果它< 5,您将交付文件并增加该值。如果它 >= 5,您将返回错误,并删除该文件。

关于php - 链接应在五次访问后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/995399/

相关文章:

javascript - 防止未定义的数组条目

php - Laravel 4 资源 Controller NotFoundHttpException

php - 如果用户已存在于 Laravel 中,如何编写返回响应?

php - Ajax 中的 RSS 阅读器 : Help with code, 有什么问题/缺失?

php - 文档格式

php - 服务端接收支付宝移动支付请求字符串

php 调用 mysql 过程未检索结果

php - 504 网关超时媒体殿

php - 如果我们在 PHP session_name 中使用点,为什么浏览器会丢失 session ?

php - php有omniauth吗?