【问题标题】:File Size Calculated but File Not Found已计算文件大小但未找到文件
【发布时间】:2015-05-31 20:34:46
【问题描述】:

我正在尝试安装位于http://www.w3webtools.com/simple-page-download-file-using-php-and-jquery/ 的“使用 php 和 jquery 的等待时间文件下载脚本”脚本

我已经下载了演示脚本并将它们安装在我的服务器/var/www/test/

当我尝试调用演示文件时,它会识别文件大小,但会说找不到文件。 http://4x4submods.tk/test/download.php?f=advance-security-login-system-using-php-mysql.zip

有什么想法吗?

mod_rewrite 已启用。

【问题讨论】:

  • 去掉url末尾的[/url]!
  • 抱歉,已删除 [url] 标签。
  • 您使用什么文件路径来获取文件大小?如果它是相同的,那么我假设有一些安全设置阻止访问该文件!
  • 该文件位于4x4submods.tk/test/data/…,因此您提供了错误的下载链接,这就是它不起作用的原因。
  • 来自教程“在根文件夹中:您需要拥有的根目录 – x.php:此 php 文件检查从文件夹“include”中的 function.php 文件创建的哈希并返回要下载的 url – .htaccess: 将 RewriteRule 添加到根文件夹中的文件 .htaccess 中。规则如下: – download.php:" 所以是的,它位于 /test/data/ 中,但这是通过配置文件。

标签: php jquery file download


【解决方案1】:

这行得通:

<?php
header('Content-Type: text/html;charset=UTF-8');
include 'include/config.php';
include 'include/function.php';

?>
<!-- Edited to point to the latest copy of jquery! -->
<script src="http://code.jquery.com/jquery-2.0.0.js"></script><?php

$fname='hello.txt';
$download=1;
if(!file_exists(UPLOAD_DIR.'/'.$fname))
{
    $download=0;
}

$downloadLink='download/'.makeHash($fname).'/'.$fname;


function file_size($url){ 
    $size = filesize($url); 
    if($size >= 1073741824){ 
        $fileSize = round($size/1024/1024/1024,1) . 'GB'; 
    }elseif($size >= 1048576){ 
        $fileSize = round($size/1024/1024,1) . 'MB'; 
    }elseif($size >= 1024){ 
        $fileSize = round($size/1024,1) . 'KB'; 
    }else{ 
        $fileSize = $size . ' bytes'; 
    } 
    return $fileSize; 
} 
?>
<div class="container">
    <span class="filename" id="fileinfo-filename" title="<?=$fname?>"><?=$fname?></span>
    <span class="fileinfo" id="fileinfo">File Size: <span id="fileinfo-filesize"><?php 
    if($download!=0){
        echo file_size(UPLOAD_DIR.'/'.$fname);
    }else{
        echo 'N/A';
    }
?></span> - Your IP: <span id="fileinfo-views"><?=$_SERVER['REMOTE_ADDR'];?></span></span>
<div id="btnX" class="btn btn-blue">
    <span class="text1" id="countdown-info">PREPARERING DOWNLOAD...</span>
    <span class="timedown" id="countdown-time"></span>

</div>
<div id="<?=$download?>" class="loading" style="display: none;"></div>
<input id="download" type="text" style="display: none;" value="<?php echo $download ?>">
<a class="btn btn-green" id="btn-download" style="display: none;" href="<?=$downloadLink?>">
    <span class="text2">DOWNLOAD</span>
</a>

</div>
<script type="text/javascript">    
    http://w3webtools.com/wp-admin/post-new.php#
    var countDown;

    jQuery(document).ready(function(){

        countDown=function(start){
            if(start==0)
            {
                jQuery('#countdown-time').html('');
                jQuery('#countdown-info').html('PREPARERING DOWNLOAD...');
                bla = $('#download').val();
                if(bla==0){
                    jQuery('#countdown-info').html('ERROR: Press F5 to try again. <br></br>404: File not found!');
                }else{
                    jQuery('#btnX').css('display','none');
                    jQuery('#btn-download').css('display','inline-block');

                }
                return true;
            }
            jQuery('#countdown-time').html(start+'s');
            start--;
            setTimeout('countDown('+start+')',1000);
        }
        countDown(5);
    });

</script>

Function.php 文件:

<?php
function makeHash($fileName)
{
    return md5( $fileName . SECURITY_CODE );
}
function verifyHash($fileName,$hashCode)
{
    return $hashCode==makeHash($fileName);
}

确保在解码时使用与密钥中相同的哈希!

其余的并不是真正需要的,并且变得比必要的更复杂!

在哈希中包含文件日期,如果文件日期由于某种原因发生更改,例如,如果您需要进行系统还原,则可能无法正常工作。如果您认为您真的想要它,您可以将服务器名称放回哈希中!

【讨论】:

  • 谢谢,但我怀疑这仍然更深入。我已经把我的文件换成了你的,但我从来没有倒计时。它只是继续准备下载:4x4submods.tk/test/download.php?f=hello.txt
  • 我刚刚点击了您提供的链接。它可以工作,只是文件不在您的服务器上,因此无法找到它!
  • 文件不在我的服务器上是什么意思?我在数据目录和测试目录中创建了一个 hello.txt 文件。它只是停留在准备下载上,所以我恢复到演示中的文件。我已将您的文件添加为 download2/function2.php,请参见此处:4x4submods.tk/test/download2.php?f=hello.txt
  • 您需要实际提供 jquery 的副本才能使用它!在文件的顶部,您将看到一个指向 js/jquery-1.11.0.min.js 的链接,您需要将其设置为您在服务器上保存 jquery 的位置以及它的文件名是什么!!!
  • 我已将答案编辑为指向 jquery 的在线副本。如果速度很重要,请下载最新副本并将其安装在服务器上,然后将链接更改为本地副本!
猜你喜欢
  • 1970-01-01
  • 2021-09-10
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多