file_get_contents failure on windows - php

I have a PHP CLI application that creates a file with file_put_contents then listens for changes to that file. If the filemtime changes then I try to get the content with file_get_contents. It often fails to retrieve the contents on windows. This baffles me. How is it possible that a process that creates the file cannot open the file?
I even ran icacls on the folder that the file is in and it still fails to have access to read the file that it created.
icacls.exe 'MYFOLDER' /grant MYUSER:(OI)(CI)F /T
Can someone please enlighten me on how to insure a PHP process can read a file it created?

Related

The action cannot be completed because the file is open in httpd.exe

I wrote JS to upload really large videos in chunks and PHP script to assemble the pieces on the server. The file process works except that I can't play the file unless I "Restart All Services" from the PHP taskbar. After that the file plays fine. If I try to rename the file in Windows Explorer I get the follow error:
The action cannot be completed because the file is open in httpd.exe
I suspect it may have something to do with never calling move_uploaded_File(), but that calling that procedure won't work on the assembled file.
Found it myself. Nothing to do with move_uploaded_File not being called. There was a syntax error that stopped the script before the file was closed. Since the server had been set to not display errors it wasn't apparent that an error occurred before the file was closed.
The temporary files are closed(explicitly) after being appended to large file being assembled. It seems that this accomplishes part what move_uploaded_file does, that is, disposing of the temporary file after using it. The script ending probably does that too.

Cannot download using fpassthru

I'm trying to serve a file for download to a user, and I'm having trouble with fpassthru. The function I'm using to download a file is:
http://pastebin.com/eXDpgUqq
Note that the file is successfully created from a blob, and is in fact the file I want the user to download. The script exits successfully, and reports no errors, but the file is not downloaded. I can't for the life of me think what's wrong.
EDIT: I removed the error suppression from fopen(), but it still reports no error. Somehow the data in the output buffer is never being told to be downloaded by the browser.
I tried your code (without the blob part), and it worked fine. I can download a binary file. Based on my experience, here are something to check:
Has the file been completely saved before you initiate the reading? Check the return value of file_put_contents.
How large is the file? fpassthru reads the whole file into memory. If the file is too large, memory might be insufficient. Please refer to http://board.phpbuilder.com/showthread.php?10330609-RESOLVED-php-driven-file-download-using-fpassthru for more information.
Instead of downloading the file to local server (reading the whole file into server’s memory, and letting the client download the file from the server), you can create an SAS URL, and simply redirect the browser to the URL. Azure will take care of download automatically. You many want to refer to http://blogs.msdn.com/b/azureossds/archive/2015/05/12/generating-shared-access-signature-sas-using-php.aspx for a sample.
I was able to download the file by passing a stream obtained with the Azure API directly to fpassthru, without creating a file. Unfortunately, I can't show the code because it belongs to a project that I have finished working on and the code is no longer available to me.

The action cannot be completed because the file is open in php.exe

I am running my batch file and i am executing my php file through batch file,now the php file will create a text file. after, i will terminate my batch file the textfile cannot be renamed or cannot be deleted. it shows this error "The action cannot be completed because the file is open in php.exe"
Thank you in advance.
This error can occur if you maintain file handles throughout your application. Make sure you are closing the file. Without seeing your PHP code I can't give you a better answer than that.

When user clicks on link I want to unzip file and then open it

I would be grateful for help concerning this issue:
User clicks on a link:
the link itself has the parameter that tells which file needs to be unzipped to /unzip folder. After a file is unzipped, I would like to open the file.
How can I do this? I have the unzip part coded already.
I can suggest a following solution:
You create a folder on the server where you will unzip the files to.
You create an .htaccess file there which specifies your own php script as 404 Error handler
In your php script you parse URL and identify which file to unzip, unzip it and redirect user to the newly created file
If you need to clean the unizpped files, you can create a cronjob which will remove files older than a certain time
What you get from that is:
File transfer from server to user is handled by web server
You actually cache your work as 404 handler won't run if you have the file in place
You can significantly lower the server load as this approach reduces the amount of operations performed on server side (when file exists)
The description above assumes Apache as a web server

move_uploaded_file returns true but file does not appear in folder

I have a couple of scripts that worked fine on another server.
Now that I have moved everything on to a new server, the file does not appear in the destination folder. The strange thing is that move_uploaded_file returns true.
Also, I printed the post data and there is no error.
Can you guess what's possibly going on. The files I am trying to upload as a test are very small ( 10 kb).
The move likely succeeded, check the following:
You don't have access to view the file.
Use chmod("/path/to/file.ext", 0755); to add view rights for the ftp user.
You moved the file to an location that doesn't store the file. /dev/null
You're looking in the wrong folder. Did you use a full path?
The file is removed shortly after the move.
In my case, Total Commander, used as an FTP client, truncated output to 10000 files in directory. When I connected via SSH using WinSCP I was able to see all ~14000 files in the directory.

Categories