ZIP generated from PHP works with winrar but not others? - php

I have some PHP code which allows users to submit files.
I then can download the ZIP file. If I use windows with Winrar, it unzips fine, I can open my files and that's all good. I have tested this on multiple computers.
If I don't have winrar and I use the default unzipper in Windows 8 or Windows 7 (or OSX), I get a message which says that the zip file is invalid.
Are there any known issues that I have been unable to find? Does anyone know what could cause this behaviour?
Thanks,
Here is a link to the output zip.
https://www.dropbox.com/s/imvzixl3y3l12l1/TestAreaForMax-aa-57.zip

Without seeing your code and ZIP file nobody will tell for sure, but I recall having similar issue and it appeared my script was throwing some error messages which ended up in produced ZIP archive. Some unzip managed to recover from this, others did not, so I suggest to open your ZIP file in any text editor and see if you got nothing like PHP Warning message in the beginning of the file.
EDIT
Regular zip file starts with "PK". Your file starts with " PK" so unless you are not really need that, remove ?> from PHP script that produces your zip file (and I'd suggest removing this from all scripts) as there's one space somewhere which does what I described above, and most likely it lurks after ?>
If you are in PHP code block, then white space does nothing. But by placing ?> you tell PHP when your scripts ends. And outside PHP code block, white space is just being sent to the client. So if you keep ?> and the space is after it then it will be sent. And if output of your script is something downloadable, said space will "join" the byte stream user saves. By removing ?> you are not telling explicitly PHP where your code ends, and in that case it will automatically end with end of source file. And white space is a bitch to track as usually it is "invisible" due to editor settings (and for that reason I set my syntax highlighting to show spaces in different color than background color is)
In general, for reason above, it is always good NOT use ?> if you do not mix PHP code with anything (i.e. HTML). It always help to configure your editor to auto-trim trailing whitespaces on save

Related

Certain mp3 files create zip error

I've been trying to make a zip archive for some mp3s. The problem I have is that some of the mp3s just break the zipping process, in bizarre ways. If I don't include one of these files everything is fine; if I put one of them in then it goes pear-shaped.
Ultimately, I want to put the zip file on my server for people to download, and I would like to make it on the fly using php. If I don't include any of my problematic files in the archive, it works fine. If I do, then I get a '500 internal server error', but nothing in the server error log. By creating my own error log for every step of the process, it's at the point I try and do zip->close that it seems to be failing.
I tried taking a step back, and making the zip archive locally using 7zip. If I include a problem file I can do this, but if I upload this zip archive to the server and then download it, the download is corrupt. If I don't include a problem file, it's fine. The same happens with winzip.
In total I have about 100 mp3s, of which about 6 cause this behaviour. They all have the same permissions, they all exist, they're all in the same directory, they're all roughly th same size. I even tried making the mp3 again from the source .wav file - the same thing happened with the new version.
I'm completely stumped. I've searched all over, and can't find any clues whatsoever. Help!!
Several reasons are possible (and they can happen together): (a) buggy code in PHP zip creation - either in your code or in code that does actual compression , (b) text mode used in uploading or downloading the files.
As you see yourself, using 7zip solves the problem with PHP zip, so there must be some error in compression code. Now if you put created ZIPs and they are downloaded as broken, this can mean that the file is provided using text mode.
One of the non-obvious reasons is enabled mbcs functions overloads in PHP. With overloads enabled, strlen and similar functions, when binary data is passed to them, start to produce unexpected results (this is because they treat input as UTF8 strings).

Can't open files generated by PHP app on windows

I'm having a problem when I try to open files that were generated by my CMS on my windows machine. It has an upload function that saves the file on a given location (it is on my user folder, where I'm supposed to have all the write and read permissions).
All I can see in that folder is a generic thumbnail, the ones generated by windows for each image type, PNGs and JPEGs in this case. I tried to change de image permissions and set it to the current computer user but it doesn't work. And I'm unable to move these files to another location (upload them to the server via FTP).
I wonder if there is a way to change this behavior.
One thing I noticed is that if I copy and paste those files, I can see its contents normally. I know I can do that to each of them and rename, but there are dozens of images in that folder... don't wanna do the same thing everytime I upload a new image.
Update
This is happening only to the images that were moved (I'm using php's move_uploaded_file function, btw), not to the ones that where generated (croped, resized and so on).
It`s happening because Windows keep file permissions of TEMP folder (usually c:/windows/temp) when moving a file.
To prevent it you need to change "upload_tmp_dir" in "php.ini" to your server directory or set wide permissions to your windows temp folder.
You probably have an error in some php file which is preventing the image from being displayed correctly.
What i would do in order to detect the problem is to comment out the header() calls in the php file responsible for displaying the image and see if there is any output other than the image data.
Inspect the whole output, it only takes a character to break everything, so check if there are no blank lines at the top or bottom of the output. And in case you have an error log, consult that, it will help you trace the problem quickly.
When i have this problem, it's usually a notice or a warning showing up during the process of the image generation, and if you haven't disabled errors from being output to the screen, the notice text will be considered as part of the image data which makes the browser unable to interpret your image.
If you'd like i can take a look closely for you but i would need to see what you are doing in that php file to be able to tell you for sure.

php code turned gibberish?

I went to edit this PHP file - it's supposed to generate those captcha security images on contact forms - the images werent working, so I was going to see if there was a broken path or something i could fix simply.
But when I opened the file it looked like this:
http://mydomainsample.com/explosion/screenshots/Screen%20Shot%202012-05-17%20at%209.34.14%20AM.png
complete gibberish.
Is it possible this happened somehow while downloading the file from the server? I did not have ftp access to the site originally - we got control of the domain and transferred it from one host to another.
I used site sucker to backup the site before transferring, but it downloads php files as html files. you end up with filename.php.html.
in the past this has never turned the php into incomprehensible gibberish, so i dont understand why it did now.
The problem is, you cannot use programs like "site sucker" to get PHP files. This is because when you get a PHP file from a URL, the file is executed, and you're getting the output of the script. That's why you get .php.html.
It doesn't "turn the php into incomprehensible gibberish", the server runs the script and you're getting the output. Most of the time the output is HTML, which you can open as text. In this case, the script's output is a PNG file, thus why you see "gibberish". Rename the file to .png, then you'll see the image.
You need to get FTP access in order to get the PHP source.
That's a PNG image, not PHP source code.
The file actually looks like a PNG image, maybe you just downloaded output the PHP script has generated?

PHP serving pdf files works at first, but begins to fail over time?

My friend and I are serving pdf files among our group via php on a link, off of a shared host. Basically, the php file doing the serving is just setting the doc type and name, and then doing a readfile on the pdf file, nothing to complicated.
At any rate, it worked great for a long time, but over time it's like the files decay or something. One at a time, they start having the following adobe reader error:
"There was an error opening this document. The file is damaged and could not be repaired"
This is true of any computer downloading the file (even first time downloading to that computer, no cache issues on the client side). The source file on the server is still in perfect condition, opens just fine, and can be copied via FTP and open on the same computer that was having issues with the downloaded version.
So, what's the deal? Is it possible the host is caching a version of the pdf that's corrupt and not releasing it? Any idea what on earth would cause this?
Thank you,
James
Just download the corrupted PDF, open it with text editor like notepad++ and check that there is nothing prepended/appended to file like html or spaces.
It should start with somthing like that
%PDF-1.4
%âãÏÓ
1 0 obj
<</DecodeParms
And ends with %%EOF
<</Info 6 0 R/Root 5 0 R/Size 7/ID [<2dc4e4e34299742156136c9f3e72d3db><1b914aa93d42277e939b341233d3e66b>]>>
startxref
3540
%%EOF
No spaces, stange chars after %%EOF. Hope it helps!
Adobe PDF Reader (as browser plugin) has a suspect way of caching files from the same URL, even if the content has changed.
Try to add a dummy timestamp or random number to the URL, so Reader is forced to load the file again.
Example:
example.com/invoiceDownload.php?rand=123123123123
I ran into this same problem -- some files opened normally, some had graphical glitches, and a few wouldn't open at all.
At the top of the PHP file I was using to serve the files, I included my site's main config file. When I removed it and included only what was necessary (path & database info), the PDFs began to serve normally.
Long story short, ensure any unnecessary code in the PHP file is removed.

PHP run code from txt file

If i had a text file on my web server, which contains full PHP code, all properly formatted, could i use PHP fopen to read the text file and echo the output of the PHP to the browser. Ie. Run PHP code that is held in a text file rather than hard coded?
Thanks
It's perfectly possible to do this (it's just another file after all), although I'd be tempted to directly include it rather that messing around with fopen/eval, etc. (N.B.: The file would of course have to be "fully formed" and begin with "<?php", etc. for the include to work.)
However, I'd be very wary of naming the file with a .txt extension as this will mean that it'll be possible to browse the contents of this file directly from the browser if it exists in the public HTTP docs area. As such, why not simply write the data into a .php file - this will be no more difficult than a .txt file and offers the advantage that it always be parsed by the web server if someone attempts to access it.
You can read in the file as you suggested using fopen you can then execute the string that is read in using eval. I wouldn't recommend this. Try another solution to what ever the actual problem is.

Categories