I'm having some issue with the unlink function.
I have a page that when refreshed, it searches a directory for newly added files. The user may choose to manage the files and can also delete any file. However, when the user deletes the file, there is almost a 5 second delay before the actual file is deleted from the server directory. In the meantime, if the user refreshes the browser, that same file that was supposed to be deleted re-appears as a new file. The issue with this is that if the user deletes this file again, the file no longer exists because of that initial delay...
Any thoughts on this? It's driving me crazy and not sure how to remedy this situation...
One solution could be to create a new file when you call unlink(), and name the new file $original_filename."_deleted". Then when you list the files, exclude any ending with "_deleted". Then you just need to worry about cleaning up all the "_deleted" files every so often with a cron job.
function my_unlink($filename){
touch($filename.'_deleted');
unlink($filename);
}
function list_files(){
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !preg_match('/_deleted$/',$entry)) {
echo "$entry\n";
}
}
closedir($handle);
}
}
clearstatchache(true, $file) still might be worth a try, though I am pessimistic after the documentation on unlink.
Maybe there are too many files in the directory, and using several directories might help (using directories with the first two chars of the file name).
My hope however goes that the listing overview page is cached. Using
header("Cache-Control: no-cache, must-revalidate");
or so might help.
Related
If there is any mistake in my writing, please excuse me. I'm not very good at translating from Spanish to English.
I am using pluploadQueue, reading the example script (upload.php). I have tested it and it works fine.
In addition to all that, I need to record information about the files that are attached in a database.
I have tried to add the statements that save the information in the database, but it only works halfway. It generates just one entry in my database table.
I notice that in the "upload" directory it generates a 1024kb .part for the 1st file but it doesn't do anything else there. Although visually the Widget tells me that it has uploaded all the files.
I need to save as many entries as files are selected (many can be uploaded, so it has been thought).
Could someone point me to a reference? I have tried to search for something about my problem but I have not been able to find anything.
I use version 2.3.9 of plupload
Correct me if I'm wrong, but I'm understanding that the upload.php script is executed (or should be) for each file to be uploaded, right?
My logic is such that:
Insert into db basic "header" information for the first time. In the following operations it is recovered.
plupload upload routine
Insert in db file information
thus forming a master-detail relationship bettween 2 tables.
For some reason, it stays at point 2. And this part would not be running:
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// If temp file is current file proceed to the next
if ($tmpfilePath == "{$filePath}.part") {
continue;
}
// Remove temp file if it is older than the max age and is not the current file
if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
#unlink($tmpfilePath);
}
}
closedir($dir);
}
Help me?
EDIT:
Okay. I have found a typo error. I corrected it and now it saves me for each file. What I can't figure out right now is why it has generated hundreds of .part files in the directory instead of overwriting it. And this led to hundreds of records being stored, one for each generated .part.
If you use a code like below to delete a file that was created by your code, where does that file get deleted too? Can the file be recovered?
function RemoveDirectory($path){
foreach(glob("{$path}/*") as $file)
{
if(is_dir($file)) {
RemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($path);
}
Let's just say I called this directory at the wrong time in the code, and I have regrets.
The file gets deleted from your hard drive. It doesn't get removed to a "Recycle Bin". It gets completely removed. To recover the file after that, you will need some sort of undelete software, which may or may not work, depending on whether or not you have overwritten those sectors of the hard drive with other files since the delete took place. If you accidentally delete a file, remove the drive immediately and boot from a different drive to prevent further writes from occurring.
I am having issue with automatically deleting files from a specific folder on my server.
I need to run an automatic delete every 31 mins on a folder which stores incoming documents. These files will always be *.pdf format.
I have found an issue similar on this site.
How to delete files from directory based on creation date in php?
However my issue is with *.pdf files and I have never used php before, ideally I was looking for a .bat file, but if that's not possible it's no problem.
<?php
if ($handle = opendir('/path/to/files')) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($file);
if((time() - $filelastmodified) > 24*3600 && strtolower(substr($file, -4)) == ".pdf")
{
unlink($file);
}
}
closedir($handle);
}
?>
This added condition checks if the filename ends with ".pdf". You could run this as a cronjob.
You might as well use shell commands instead. find with -name, -exec and -mtime does the same and saves the need for a PHP parser.
Using PHP, I want to get the content from the first file in a folder and when the content is loaded, delete the file. Here is what I have:
$a = opendir('./');
while (false !== ($entry = readdir($a))) {
if($entry != '.' && $entry != '..') {
echo $entry = file_get_contents('./'.$entry.'', FILE_USE_INCLUDE_PATH);
break; // only need the first file
}
}
The code above loads the first file in the folder and I can delete it successfully using something like
unlink("temp.txt");
So there are no permission denied errors. BUT what I need to do is delete the file by its variable name (because every filename is different). Surprisingly for me, unlink("$entry"); or something similar does not let me delete it, instead showing a warning along with the first few lines of the content of that file. If I echo $entry It shows temp.txt correctly. Can someone enlighten me? What am I missing here?
(Optional (un)related question: If I have numeric files like 1.txt, 2.txt, 3.txt, 10.txt... . Is there a way I could modify the code above in a way, that it does not load files like 1,10,2,3 ..., instead load it like 1,2,3,10...?)
UPDATE:
The updated code that works (for future reference):
$a = opendir('./');
while (false !== ($entry = readdir($a))) {
if($entry != '.' && $entry != '..') {
echo $b = file_get_contents('./'.$entry.'', FILE_USE_INCLUDE_PATH);
break; // only need the first file
}
}
unlink("./$entry");
The first problem is that you're overwriting $entry with the file contents, as such the filename is no longer valid when trying to delete it (explaining the error with the file contents).
Secondly, because you're using FILE_USE_INCLUDE_PATH you don't know exactly where the file is located, and unlink resolves related to the current working directory, which is most probably not $a.
Use unlink($a.'/'.$entry) and you'll be fine.
As for the unrelated question - use scandir to get all the files in the folder, then apply natsort to the resulting array to sort by a 'natural sorting algorithm'. Keep in mind that a directory listing always also lists the folders . and .. which you'll have to detect and skip or remove manually.
I'm not a developer, but I'm the default developer at work now. : ) Over the last few weeks I've found a lot of my answers here and at other sites, but this latest problem has me confused beyond belief. I KNOW it's a simple answer, but I'm not asking Google the right questions.
First... I have to use text files, as I don't have access to a database (things are locked down TIGHT where I work).
Anyway, I need to look into a directory for text files stored there, open each file and display a small amount of text, while making sure the text I display is sorted by the file name.
I'm CLOSE, I know it... I finally managed to figure out sorting, and I know how to read into a directory and display the contents of the files, but I'm having a heck of a time merging those two concepts together.
Can anyone provide a bit of help? With the script as it is now, I echo the sorted file names with no problem. My line of code that I thought would read the contents of a file and then display it is only echoing the line breaks, but not the contents of the files. This is the code I've got so far - it's just test code so I can get the functionality working.
<?php
$dirFiles = array();
if ($handle = opendir('./event-titles')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dirFiles[] = $file;
}
}
closedir($handle);
}
sort($dirFiles);
foreach($dirFiles as $file)
{
$fileContents = file_get_contents($file);//////// This is what's not working
echo $file."<br>".$fileContents."<br/><br/>";
}
?>
Help? : )
Dave
$files = scandir('./event-titles') will return an array of filenames in filename-sorted order. You can then do
foreach($files as $file)
{
$fileContents = file_get_contents('./event-titles/'.$file);
echo $file."<br/>".$fileContents."<br/><br/>";
}
Note that I use the directory name in the file_get_contents call, as the filename by itself will cause file_get_contents to look in the current directory, not the directory you were specifying in scandir.