Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting filename (or deleting file) using file handle

Tags:

file

php

Is there a possibility to obtain filename from file handle? Or how can I delete file having only a handle?

like image 612
ts. Avatar asked Feb 28 '11 16:02

ts.


People also ask

What happens to inode when file is deleted?

Space and inodes may not be returned to the system until the volume blog has processed the deleted blocks.

Is file object and file handle same?

The file handle is used to identify the file in many function calls. Each file handle and file object is generally unique to each process that opens a file—the only exceptions to this are when a file handle held by a process is duplicated, or when a child process inherits the file handles of the parent process.

What happens when a file is deleted while a process is writing data to the same file on Linux?

When you open a file, it gets loaded in the open file table this tables exists in the computer memory (RAM), after that all modifications made to that file are only saved to the same file loaded in that table and not the one on the disk, even if you delete your file from the disk it still exists in the open file table.


2 Answers

There is stream_get_meta_data. It works for a stream that you get from tmpfile(). If you call it on a regular file pointer then you might only get the basename.

$meta_data = stream_get_meta_data($stream_or_file_pointer); $filename = $meta_data["uri"]; echo $filename; 

Example for tmpfile():

"/private/var/folders/v3/n54x13jx5v7610fw9dm0wcxm0000gn/T/phpCJvevP" 

Example for fopen("somefile", "r"):

"test" 
like image 126
lion.vollnhals Avatar answered Oct 08 '22 05:10

lion.vollnhals


Nyes. Afaik there is no function in PHP to that directly. But on Linux, you can do

$fp = fopen('somefile', 'r'); $stat = fstat($fp); $inode = $stat['ino']; system("find -inum $inode", $result); echo $result; 

This is untested so it might need tweaking.

EDIT Apparently, there is a simpler solution.

like image 21
Gordon Avatar answered Oct 08 '22 03:10

Gordon