dongzouqie4220 2018-05-21 21:09
浏览 326
已采纳

php:if(file_exists($ file))对于某些文件返回true,对其他文件返回false

i'm stumped. i'm putting together a script to take PATH_INFO from a URL and use that to send files from a particular directory on the server.

on the server:

ll /path/to/directory/with/my/files/
total 7195210
-rwxrwx--- 1 user group  716852833 May 11 15:17 file1.7z
-rwxrwx--- 1 user group 1000509440 May 11 15:31 file2.cxarchive
-rwxrwx--- 1 user group 5878056960 May 11 17:32 file3.ISO

i have an if (file_exists($file) block in my code, and it works for file1 and file2, but file3, that's in the exact same location, it triggers the else statement, indicating that PHP thinks that file doesn't exist.

<?php
//get file name from URL, trim leading slash.
$filename = ltrim($_SERVER['PATH_INFO'], "/");

//sanitize the filename
if(preg_match('/\.\.|[<>&~;`\/$]/', $filename)) {
    trigger_error("Invalid path '$filename' attempted by $user");
    show_error();
}

//prepend my files directory to the filename.
$file = '/path/to/directory/with/my/files/' . $filename;

//send file
if (file_exists($file)) {
    echo '<pre>The file '; print_r($file); echo ' exists.</pre>';
    header('X-Sendfile: $file');
    exit;
} else {
    echo '<pre>the file does not exist?</pre>';
    show_error();
}

?>

so if i browse to the following URLs on my server:

https://my.server.com/script.php/file1.7z

The file file1.7z exists.

https://my.server.com/script.php/file2.cxarchive

The file file2.cxarchive exists.

https://my.server.com/script.php/file3.ISO

The file does not exist?

a bunch of testing results in the likely culprit being that the file is large. i get that with sending files memory limits are an issue, but how do i get PHP to see that this (large) file exists?

  • 写回答

1条回答 默认 最新

  • doudi1449 2018-05-22 13:44
    关注

    based on @user3783243's comment above:

    Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

    -https://secure.php.net/manual/en/function.file-exists.php

    so i wrote my own file_exists function without that limitation (based on a comment on that page):

    function fileExists($file){
    return (@fopen($file,"r")==true);
    }
    

    then, inserting it into the code:

    //send file
    if (fileExists($file)) {    //remove underscore, cap E
    echo '<pre>The file '; print_r($file); echo ' exists.</pre>';
    header("X-Sendfile: $file");    //fixed, thanks @jamie
    exit;
    } else {
    echo '<pre>the file does not exist?</pre>';
    show_error();
    }
    

    success! files that exist work (even big ones), files that do not run show_error().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据