r/PHPhelp • u/mapsedge • Aug 29 '25
PHP can write files, but can't rename a temp file?
Windows Server 2019, IIS 10, PHP 8.3
Please note: I work with what my employer gives me. Telling me Windows is bad isn't helpful. (And in any case, "php on windows is bad" is outdated and incorrect. Personally, I'm a linux user, so I KNOW what the objections are. They're just wrong.)
In short:
Ghostscript creates a new PDF from an old PDF.
PHP tries to rename the new PDF with the old PDF name. According to what I read, rename will overwrite the old file if it exists. The rename doesn't happen.
Givens:
PHP, IUSR, IIS_IUSRS all have write access to the folders and files in them.
The temp file is created successfully.
PHP can create files, e.g. touch("c:\\path\\to\\file.txt");
PHP can unlink files.
The file created by ghostscript is owned by IUSR.
Issue:
PHP can't rename the new file. Is ghostscript keeping a lock on the temp file?
I'm stuck where I am until I find a way around or through this issue.
EDIT:
With further experimentation, I'm unable to do anything with the temp file created by ghostscript. Even with the original file gone, the temp file won't rename, won't copy. So is ghostscript holding a lock on the temp file?
Adding "sleep"s to leave room for locks to be released didn't help either.
<?php
$gsPath = 'C:\\path\\to\\gswin64c.exe';
$tempOutput = $filePath . '-temp.pdf';
// Build the Ghostscript command
$cmd = "\"$gsPath\" -dCompatibilityLevel=1.4 -sDEVICE=pdfwrite -o \"$tempOutput\" \"$filePath\" -dBATCH -dNOPAUSE";
// Execute the command
exec($cmd, $output, $returnVar);
if ($returnVar === 0 && file_exists($tempOutput)) {
// Overwrite original with converted PDF
$retval = rename($tempOutput, $filePath);
if($retval) {
touch($filePath);
echo "Converted $filePath to PDF version 1.4<br>";
echo "<pre>[789114] file_exists(\$tempOutput): " . print_r(json_encode(file_exists($tempOutput)), 1) . "</pre>"; // should be false
} else {
echo "<pre>[238303] Unable to rename the temp pdf\n</pre>";
}
return true;
} else {
// Conversion failed
echo "Failed to convert $filePath to PDF 1.4<br>";
if (file_exists($tempOutput)) {
unlink($tempOutput);
}
return false;
}
4
u/MateusAzevedo Aug 29 '25
From the docs:
Even when PHP has write permission to the folder, it's possible that the file itself isn't writable. Make sure you have error reporting set to
E_ALLand check ifrename()emits any warning.Is
$filePathan absolute or relative path? If it's relative, there is a possibility it's referencing the wrong location. You can check withrealpath().2
u/flyingron Aug 29 '25
Can you do the same thing from a command prompt?
Something may be holding the file open and on windows that will prevent it being deleted. While the PHP docs say it "overwrites" the destination, it really deletes the destination so it can rename the source file to it.
1
u/isoAntti Aug 29 '25
how about instead of renaming make a copy of the file
1
u/ITGuy424242 Aug 29 '25
I would suspect AV or windows defender etc would be holding the original file
That or the software doesn’t have write permission on the folder that holds these files?
1
u/[deleted] Aug 29 '25
Have you tried renaming the original file (or deleting it) and then renaming the new file?
2
u/NoDoze- Aug 29 '25
What's up with all these recent posts with people using php on Windows? Then they're wondering why they're having issues. Microsoft officially stopped supporting php 8 and newer a few years ago. These installs are using third party packages, which tend to not be bug free. Quit making things difficult for yourself and run php native on Linux.
2
u/obstreperous_troll Aug 29 '25
Try using Process Explorer and searching for anything holding open a handle (should be something under the
Findmenu for that).2
u/zovered Aug 30 '25
You’re almost certainly hitting a Windows file-handle lock. If the file inherits read-only, Windows can block operations. Use
clearstatcache()and, if needed,chmod($tempOutput, 0666)exec()waits for the process it starts, but you’re not capturing stderr, and if GS spawns a child or you hit an error, you won’t see it. Useproc_open()and block until it’s closed; also redirect stderr to stdout so you get messages.2
u/HolyGonzo Aug 30 '25
There is a little vagueness in your post, so a few clarifying questions:
1
u/Jin-Bru Aug 30 '25
During the sleep period are you able to rename it from within a windows terminal? Under a user context.
If you run the AppPool as Administrator does that work?
0
u/[deleted] Aug 29 '25
[deleted] — view deleted comment