In PHP, after assigning a file pointer resource to a variable with fopen(), how can I get the file name from the variable?

前端 未结 3 460
自闭症患者
自闭症患者 2021-01-21 01:42

For example:

$file = fopen(\"File.txt\", \"r\");
$filename = $file->basename;

if there was a method like basename for file objects (file poi

相关标签:
3条回答
  • 2021-01-21 02:11

    I came to this page while trying to find out the filename that PHP's tmpfile() function had created - it returns a pointer. I wanted to use it to create a file I could use with some Unix commands. In the end I used tempnam(realpath(sys_get_temp_dir()), "piclib_"); instead.

    0 讨论(0)
  • 2021-01-21 02:19

    No, there is not.

    By the way, in which scenario is this thing needed?

    0 讨论(0)
  • 2021-01-21 02:35

    No, there is not a method to do. You should rather store the filename in a variable, like this:

    <?php
    $filename = "File.txt";
    $file = fopen($filename, "r");
    $basename = basename($filename);
    

    Also, a little side note: a file pointer is not an object, it is a resource, which you can see by passing it to var_dump() (it would output something like resource(3) of type (stream)). This means that you cannot use it directly, you would have to use functions from the PHP core or a PHP extension to handle it. In the case of file pointers, you would use functions like fread(), fwrite() and fclose() to do so.

    0 讨论(0)
提交回复
热议问题