Hello:
For what you say and without posting your code, it seems a coding
problem of the passed parameters, I have created the routine you need
and if you remove the urlencode and urldecode functions, you will
notice problems with files named mary&jane.txt and other files which
contain chars that must be scaped, here it's the code:
<?php
if (isset($thing)) //If thing has contents
{
$thing=urldecode($thing); //Decode the string
echo "contents $thing<br><bt>";
readfile($thing); //Dumps out the file
}
else
{
$path='./'; //Look for files on current
dir
echo "Contents of $path<br>";
$dir = opendir($path);
while($item = readdir($dir))
{
if($item=="." or $item=="..") continue;
if (is_file($path.$item))
{
$url=$PHP_SELF.'?thing='.urlencode($path.$item);
echo "<A HREF=\"$url\">$item</A><br>";
//Dumps a link with a encoded file
}
}
closedir($dir);
}
?>
The general rule is to encode every string parameter you pass to
another script. Here are the links for the functions you need to use:
urlencode
http://www.php.net/manual/en/function.urlencode.php
urldecode
http://www.php.net/manual/en/function.urldecode.php
I have answered your question because I have posted a piece of code
that performs what you want. But if you have any other problems, don't
hesitate to request for any clarification, I will be more than happy
to assist you.
Regards. |
Request for Answer Clarification by
washer-ga
on
29 Aug 2002 02:29 PDT
hi. i don't have any spaces in $thing variable. also $thing has no
special characters and is of the form numbers.extension such as
7345.ext
below is the relevant code where $file = $thing
the urlencode/urldecode didn't work, but it is included below.
<html>
...
<?
if ($file!="") {
$file = urldecode($file); // added after viewing your answer
$filename = $file;
set_time_limit(180);
if ($compression == 1) $file=gzread(gzopen($path.$file, "r"),
10485760);
else $file=fread(fopen($path.$file, "r"), 10485760);
$query=explode(";#%%\n",$file);
for ($i=0;$i < count($query)-1;$i++) {
mysql_db_query($dbname,$query[$i],$conn) or die(mysql_error());
}
echo "<b>$filename successfully restored!</b>";
}
?>
...
<?
$dir=opendir($path);
while ($file = readdir ($dir)) {
if ($file != "." && $file != ".." && eregi("\.sql",$file)) {
// added after viewing your answer
echo "<tr><td>$file </td>";
echo "<td align=\"right\"> ";
$file_size = round(filesize($path.$file) / 1024, 2);
echo "$file_size KB </td>";
$date = date("Y-m-d H:i",filemtime($path.$file));
echo "<td> $date</td>";
$file = urlencode($file);
echo "<td> <a
href=\"foo.php?file=$file\"><b>Restore</b></a> </td>";
echo "<td> <a
href=\"$dir/$file\">View/Download</a></td> </tr>";
// dropped after viewing your answer
// echo "<tr><td>$file </td>
// <td align=\"right\"> " . round(filesize($path.$file) /
1024, 2) . " kB </td>
// <td> " . date("Y-m-d H:i",filemtime($path.$file)) . "</td>
// <td> <a href=\"foo.php?file=$file\"><b>Restore</b></a> </td>
// <td> <a href=\"$dir/$file\">View/Download</a></td> </tr>";
}
}
closedir($dir);
?>
...
</html>
i am still getting page cannot be displayed. i know i can do the above
from shell, but i would really like to figure out the problem. thanks
for the urlencode/urldecode tip though.
|
Clarification of Answer by
joseleon-ga
on
29 Aug 2002 02:37 PDT
Hello:
At a first glance, there's nothing strange on your code, could you
please, post the entire source? Also I would need some of your data
files to test it on my local system. Could you post an url to download
all I need?
Regards
|
Request for Answer Clarification by
washer-ga
on
29 Aug 2002 03:10 PDT
hi. thanks for the offer. the first ... above has the minimum html to
start the file and a <? ?> where i set my variables, the latter of
which i cannot post for obvious reasons. the second ... is just html
table stuff, nothing else. the third ... is the minimum html to end
the file. other than the first ... which has a <? ?> to set my
variables, there is only html code, none of which contains variables.
so really, for all intents and purposes, the 'entire' code is posted
above. the data that i am using is from my database, which again for
obvious reasons, i cannot distribute. perhaps, if the above code is
good, then there is a different problem? this has really got me
stumped.
|
Clarification of Answer by
joseleon-ga
on
29 Aug 2002 03:49 PDT
Hello:
I'm investigating with the source you gave me, but right now, in
this line there's a problem:
echo "<td> <a
href=\"$dir/$file\">View/Download</a></td> </tr>";
Change it by this line:
echo "<td> <a
href=\"$path$file\">View/Download</a></td> </tr>";
Because $dir is a PHP resource you created using OpenDir.
I keep investigating.
Regards.
|
Request for Answer Clarification by
washer-ga
on
29 Aug 2002 04:32 PDT
hi. thanks. sorry for that. it is a typo i made when stripping out
identifiable vars before posting. i actually have that hard coded as
follows: folder_name/$file instead of $dir/$file but good catch! i
should have known better.
i think the problem lies in these lines:
$file=gzread(gzopen($path.$file, "r"), 10485760);
$file=fread(fopen($path.$file, "r"), 10485760);
the reason i think this is the problem is that when i did a little
hard code of those lines i get the same page cannot be displayed msg.
my host's php version is 4.1.2, if that helps.
|
Request for Answer Clarification by
washer-ga
on
29 Aug 2002 05:00 PDT
now i'm certain that that is the problem. i'm really curious as to why
what i had doesn't work, but if i use the below then it works. any
ideas?
<?
class foo_files {
var $filename
var $text
function read() {
$fp = fopen($foo->filename, "r");
$foo->text = fread($fp, filesize("$foo->filename"));
fclose($fp);
return $foo->text;
}
}
?>
<?
$get_file = new foo_files($path$file);
$file = $get_file->read();
?>
|
Clarification of Answer by
joseleon-ga
on
29 Aug 2002 05:32 PDT
Hello:
Let's wait a moment,
$file=fread(fopen($path.$file, "r"), 10485760);
Are you trying to read a file of 10,485,760 bytes in a chunk?
Check this section on your php.ini file
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 30 ; Maximum execution time of each script,
in seconds
memory_limit = 8M ; Maximum amount of memory a script may consume
(8MB)
These are the default settings and if you pass it, you will get the
error you describe. Sorry for not to see it the first time, but I was
trying to reproduce the situation on my computer and without your big
file I was unable to do it.
If this is your problem, you will need to read and process your file
in smaller chunks or change this setting to allow consume more memory.
This setting is to prevent a process to hang the server.
Regards.
|
Request for Answer Clarification by
washer-ga
on
29 Aug 2002 06:29 PDT
>> Are you trying to read a file of 10,485,760 bytes in a chunk?
no, my file is not that big, but i thought fread() was meant to read
up to min(EOF,10485760).
>> max_execution_time = 30 ; Maximum execution time of each script,
>> in seconds
i thought it was possible to extend this via set_time_limit().
>> memory_limit = 8M ; Maximum amount of memory a script may consume
>> (8MB)
i don't even come close to needing 8MB for this script. ;-)
>> These are the default settings and if you pass it, you will get the
>> error you describe.
i picked 10485760 in the command $file=fread(fopen($path.$file, "r"),
10485760); because 10485760 is my server upload_max_filesize. then i
got to thinking about this number and changed it to 10, reran my
original script, and sure enough, the script ran without any page
cannot be displayed msg, and i output these first few characters of my
file on screen.
i took out my class, replaced my original code using
filesize($path.$file) instead of 10485760, and sure enough the script
worked. thanks for helping me find the problem. before this problem
was solved, my httpsd shown in top was only there for a second, and
there were no errors in my logs. this bug was worse than an internal
server error. at least then you _might_ get some useful info to help
with debugging. ;-)
|
Clarification of Answer by
joseleon-ga
on
29 Aug 2002 06:43 PDT
Thanks for your words, and I'm happy your problem is solved now! I
don't know exactly how the fopen function works at low level, but I
think if you specify you want to read 10485760 bytes at max, it tries
to allocate that memory before read, so this could be the reason why
it works now. And yes, you can extend the execution time using
set_time_limit(), the value on the php.ini file is just the default
setting.
Thanks again and I hope to be more helpful the next time.
|