PHP - Can write to file, but not read - php

I have a PHP script, which attempts to read a file and output it, but none of the ways I know are working. It's kind of a weird case, since I can WRITE to the file, but not read it.
Posting any code would probably be unnecessary, I simply tried reading the file, first with file_get_contents, and then with fopen. None of them rises an error, they simply return an empty string. Even filesize returns 0 as the length. The only thing, that works is filemtime.
Any ideas why writing would be possible, but not reading?
EDIT:
It seems like file_get_contents works, when I point to the file, directly, using FTP, although a new weird issue came up: before the while-loop in my original code, it does return the file's text, but IN the while loop, it doesn't. And it's printing anything else with no problem, so it's not an echo-related issue..

Related

Catch py_compile.PyCompileError, and convert to string

I'm trying to compile a python script and record any syntax errors
I have a script called test.py that looks like
#!/usr/bin/python
import py_compile
try:
py_compile.compile("answer.py")
except py_compile.PyCompileError as e:
print(e)
answer.py is just a self contained script that doesn't do anything except print something(for now)
i also have a php exec command in a php file that runs test.py, problem is php exec doesn't get returned anything by
print(e)
I'm guessing because e here in PyCompileError isn't a string or something weird with the format, the result simply comes back empty "". When I played around with it it seems to be an object, not sure how to convert that to a string.
I tried putting it into a string but it's not working, if I change e into a string like print("You have errors") I get the correct output, so it's not my php but I want the syntax errors on the lines. Not even sure if I should be using compile() for this
I figured it out, basically I needed doraise=True and then get print(e.exc_value)
I did initially have doraise=True before people think I was lazy and not reading the documentation, but I didn't realize exc_value was the variable that had the message(I was trying to get e.msg) so I turned it off since it said by default it prints to stderr with it off, and I tried to work from there.
Please ignore

PHPExcel outputs garbled text

Like many others out there i have had my fair share of issues trying to download an Excel file output by PHPExcel.
What happened in my case was whenever I wanted to download a file using
$obj->save('php://output')
i always used to get garbled text in my excel file with a warning saying my file was corrupt. Eventually i resolved the issue. The problem being i had a
require('dbcon.php')
at the top of my php script. I just replaced that with whatever was there inside dbcon.php and it worked fine again.
Though the problem is solved i would really like to know what caused the problem. It would be great if anyone out there could help me out with this one.
Thanks.
If you get that error - you should follow the advice we always give in that situation: you use a text editor to look in the generated file for leading or trailing whitespace, or plaintext error messages - and then in your own scripts for anything that might generate that such as echo statements, blank lines outside ?> <?php, etc.
Another way of testing for this is to save to the filesystem rather than php://output and see if you get the same problem: if that works, then the problem is always something that your own script is sending to php://output as well.
Clearly you had a problem along those lines in your dbcon.php file. This can be as simple as a trailing newline after a closing ?> in the file...
Tanmay.
In situations like your's, there can be couple of reasons for broken output:
in file dbcon.php can be a whitespace before opening or ending php
tag, so that produce some chars to output and can make file broken
(this is reason for using only opening tag in php 5.3+);
maybe file dbcon.php wasn't found by require, so you got error message in otput;
any other errors or notices or warnings in dbcon.php, because presence of global vars from current file..

What does this script do?

I have a website where users can upload files to share with others. But first I need to verify them.
Lately someone uploaded a .php file with the following commands:
‰PNG
<?php
eval(gzinflate(base64_decode("very large strings of characters")));
?>
I figured it might be harmful, so I didnt open it.
Does anyone have any idea what it does?
nobody can tell you, just do
<?php echo gzinflate(base64_decode("very large strings of characters")) ?>
to see what it would do....
edit: well now that you've posted the whole string i decoded it and pasted it here
Seems like the attacker's code was base64 encoded and gzipped.
So first the code is decoded from base64 encoding, and then it is unzipped basically until a string of code.
And then eval is called on the resulting string, which will execute the code that has been decoded and unzipped.
But without seeing what code gets generated, it is hard to say what it will do when the code is run.
I decoded the encoded text. Using the following approach
(I guess writing to file was a bad idea now that I think of it. Mainly if you're on Windows. I guess it is a bit safer on Linux with the execute bit turned off. So I was kind of lucky in this case!)
<?php
$test = gzinflate(base64_decode("encoded_text"));
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, $test);
fclose($fh);
I wrote the output to file just in case there was some random html or javascript that could infect my computer if I just echoed it to my browser. That may be why you got an anti-virus warning.
I'm not sure what it does yet.
Just skimming through the code, which is like 4,750 lines of code, it seems like it sets up Basic Auth. And then there's a lot of database functions and some basic html interface. This in PHP. There's also some perl too. Near the end.
Basically what it seems to do is this: Every page where that image is displayed it will output parts of that code and execute it along with your code, and it will try to get input data, or try to find session data and or database values.
Then other parts of the code basically create an admin interface when the url is visited like this: url?admin=1, which brings up a Basic Auth authentication. And then there is an simple interface phpmyadmin like interface where the user can try out different queries and gather out metadata about your db. Probably other stuff run to exec, etc too.
I could be wrong, but that's the gist I get from going through the code.
The code is fine the only thing you need to take care is the long string that is encrypted
< ?php eval(gzinflate(base64_decode("very large strings of characters")));
for the reference of this kind of the statement you can refer to
http://php.net/manual/en/function.gzinflate.php

strange thing, ajax response includes whitespace

I ave several JS functions that call different php files with $.ajax jquery method... yesterday everything was fine, but after cleaning up my code, i don't know what i did, but now the ajax response is like "[space]data" instead of "data"..
i could use a trim function in Js but i want to fix the source of the problem...
all my php files have the missing last ?> in order to avoid that, and before <?php i'm sure, just checked, there is no space...
how come did I introduce this error? is the server? the browser?
The funny thing is that yesterday i cleaned my code with JSLINT..! bad idea..
thanks
When I had the same problem it was just a carriage return or space after the closing PHP tag, a surprisingly easy thing to introduce by accident.
Make sure you open the PHP tag at the start of the first line of your script, close it at the end and delete everything after the closed tag (should be easy to spot in a good editor).
I can see no reason why not closing your PHP tag wouldn't just be really annoying.. but thats just me!
I know I'm late this this thread, found it because I had the same issue. I was able to confirm that the although you may clear all the spacing in your callback function it is still possible to get these white-space/carriage returns in your response text (you can even see this in chrome developer tools under the "Network" tab).
So I tested and found that if you put ob_clean(); at the top of your callback function then it clears any of those spaces. I don't know much about this function just that it was mentioned in the codex (http://codex.wordpress.org/AJAX_in_Plugins#Debugging). Hope that helps anyone else that find there way here because of the same issue
If your response from the server includes the extra space, then the php code is the cause. If your response does not include the extra space, then the problem must be in javascript.
Use Fiddler to examine the actual response from the server to see if it really has the space in it. You'll then know for sure if the problem is with PHP or JS. Then you can post the relevant code.

Is there a php function for using the source code of another web page?

I want to create a PHP script that grabs the content of a website. So let's say it grabs all the source code for that website and I say which lines of code I need.
Is there a function in PHP that allows you too do this or is it impossible?
Disclaimer: I'm not going to use this for any illegal purposes at all and not asking you too write any code, just tell me if its possible and if you can how I'd go about doing it. Also I'm just asking in general, not for any specific reason. Thanks! :)
file('http://the.url.com') returns an array of lines from a url.
so for the 24th line do this:
$lines = file('http://www.whatever.com');
echo $lines[23];
This sounds like a horrible idea, but here we go:
Use file_get_contents() to get the file. You cannot get the source if the web server first processes it, so you may need to use an extension like .txt. Unless you password protect the file, obviously anybody can get it.
Use explode() with the \n delimiter to split the source code into lines.
Use array_slice() to get the lines you need.
eval() the code.
Note: if you just want the HTML output, then ignore the bit about the source in step 1 and obviously you can skip the whole eval() thing.

Categories