...

View Full Version : Resolved File path proglem on upgrade from 4 to 5



1andyw
02-08-2010, 01:02 PM
My ISP just upgraded from php 4.4.4 to php 5.1.6. Apps that were without problems are now an issue.

We have been loading office documents into a file above the root. Now the word documents when loaded from the site onto the local pc contains error messages and not the text that was in the document. The path must be correct because the document title is read into the web page. The error message has the document title repeated in the file path and I can't figure out why. The document title is:
greetings.doc. The link that is written into the web page is the same. The link displayed in the status bar when I hover over the link is as it should be.
Error

<b>Warning</b>: readfile(../../../script/users/Vol Group/greetingsmon.docgreetingsmon.doc) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>../../../bank/doc.php</b> on line <b>18</b><br />

code:
<?php
ob_start();
if (!isset($_SESSION['username']))
{
die ("You are not signed in. Please use the 'back' button to sign in correctly.");
}
if(isset($_GET['id']))
{ //this section handles file download
$fileLocation = '/../../../script/users/' . $_SESSION['username'] . '/' .
$fileName = $_GET['id'];
$file = ($fileLocation . $fileName);
header ("Content-type: octet/stream");
header ("Conent-type: application/vnd.ms-word");
header ("Content-disposition: attachment; filename=".$fileName.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
}
if(isset($_POST["submit"]))
{ //this section handles file delete
$filetogo ="../../../script/users/{$_SESSION['username']}/{$_POST['remove']}";
unlink($filetogo);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Document Files for <?php echo "{$_SESSION['username']}";?></title>

</head>
<body><div id="container">
<h1>Your Document File, <?php echo "{$_SESSION['username']},"?></h1>
<h3><?php echo (date('l F j, Y')) ; ?></h3>
<h3><?php echo "{$_SESSION['username']}"?></h3>
<hr style="width: 50%; text-align: center" />
<table">
<tr><th style="font-weight: 700">File Name</th><th style="font-weight: 700">File Size</th><th style="font-weight: 700">Date Modified</th><td style="font-weight: 700">Delete this File</td></tr>
<?php
error_reporting(E_ALL);
$dir = "../../../script/users/{$_SESSION['username']}/";
if($dp = opendir($dir))
{
while(false !==($file = readdir($dp)))
{
if($file !== "." && $file !== "..")
{
$Im = date('F j, Y', filemtime($dir.$file));
$fs = filesize($dir.$file);


print "<tr><td ><a href='docfile.php?id=$file'>$file</a></td><td style='text-align: center'>$fs</td><td style='text-align: center'>$Im</td><td><form action='docfile.php' method='post'><input type='hidden' name='remove' value=$file /><input type='submit' name='submit' value='Delete this File' /></form></td></tr>\n";
}//endif
}//endwhile
}//endif
print "</table>";
closedir($dp);//Close the directory
?>
<hr width="80%" align="center" />
<form action="uploader.php" method="post" enctype="multipart/form-data" />
<table summary="File Upload Table" cellspacing="0" cellpadding="0" align="center" width="80%" />
<caption style="font: bold 100% Verdana">Upload New Files</caption>
<col width="30%" />
<col width="65%" />
<tr><td class="text-align: center">File to Upload:</td><td><input type="file" name="file" size="45" style="text-align: center" /></td></tr>
<tr><td><input type="reset" name="reset" value="Reset" /></td><td><input type="submit" name="upload" value="Upload File" /></td></tr>
</table>
</form>
</div><!--end div container-->
</body>
</html>
<?php
ob_end_flush();
?>


The new setup for php5 has magic_quotes_gpc off where the setup previously had that set on. Any difference?
Thanks,

Andy

JAY6390
02-08-2010, 01:10 PM
Have the files been moved at all with the new installation? It sounds like you've just got your file paths changed when the upgrade occurred. Is there any reason you can't use a full path to the files also?

Fou-Lu
02-08-2010, 01:12 PM
Drop the starting / from the $filelocation variable. The / starting will look from system root, which cannot go back up.
Also be aware that these file paths are always relative from the executing script, not necessarily from this script. If you include this script in another file which is in another directory, it will fail to load these paths. You can resolve this by always getting this directory first with dirname(__FILE__) . '/../upper/path/to/directory/'; instead. Note that when you're fetching relative against a dirname, you must start that with the /.

SKDevelopment
02-08-2010, 01:19 PM
$fileLocation = '/../../../script/users/' . $_SESSION['username'] . '/' .
$fileName = $_GET['id'];


Maybe I do not understand something ... readfile() reads files by the File System path, not by Web Server path. So the path /../../../script/users/ would be a subfolder of a folder 3 folders above the File System root ... Root is root. I always thought nothing could be above it ... Maybe "/" at the very beginning of the path is a wrong thing ? (Edit: has been already mentioned by Fou-Lu while I have been typing)

Also I do not understand why the 1st line of these 2 ends with "." ... It means the 2nd line is concatenated with the 1st one. Have you shown some edited version of the code ?

Anyway I would try to use an absolute path from the root (without '..').

Fou-Lu
02-08-2010, 01:34 PM
Maybe I do not understand something ... readfile() reads files by the File System path, not by Web Server path. So the path /../../../script/users/ would be a subfolder of a folder 3 folders above the File System root ... Root is root. I always thought nothing could be above it ... Maybe "/" at the very beginning of the path is a wrong thing ? (Edit: has been already mentioned by Fou-Lu while I have been typing)

Also I do not understand why the 1st line of these 2 ends with "." ... It means the 2nd line is concatenated with the 1st one. Have you shown some edited version of the code ?

Anyway I would try to use an absolute path from the root (without '..').

Good call on that ending ., I completely overlooked that. Technically, the next character should be a '1', since assignment is almost always true. I'm thinking it was previously concatenated with the id from get (which you should validate btw to prevent upper movement in the OS), which would indeed indicate that a modification has happened.

1andyw
02-08-2010, 07:50 PM
Also I do not understand why the 1st line of these 2 ends with "." ... It means the 2nd line is concatenated with the 1st one. Have you shown some edited version of the code ?

Anyway I would try to use an absolute path from the root (without '..').

I removed the "." from the statement end. I used the path in the Environment document_root, eliminating the "../../"

These actions removed the error code and replaced it with a scrambled word document.

Now when I click the link to a doc within the file, a dialog box offered to open or save the file. Either way, I end up with MS Word displaying the following:


ࡱ > , . + 7

bjbjU U & 7| 7| l D D D D D D D X

X $ D D D D D D D
X * b F 0

X X D D D D
Yr

The Welcome page has link to the existing files for the Group. Click this link and view the current list of files.

The file list has these headings: File Name, File Size, Date Modified, Delete this File.


What do you do with the changed document? If you want to upload the document, you may wish to leave the original alone and put your copy in the same folder with a version indicator. If so, just add ver1 or ver2 to the current name and upload that to the Vol Group folder.

If you wish to upload a document, ensure first, that a document with the identical name does not exist since this will cause an error box.

If the name is already in use and is in the Vol Group folder, use the Delete this File button to delete the file, then upload the new document. There is now recovery of a deleted file. My suggestion is to change the name slightly using ver1 or something, then upload your file. When that has gone as you wish, then delete the existing file.




e f *

0J CJ aJ j CJ U aJ j CJ U aJ CJ aJ * + ) *

-
.






$ &P 1h / =! " # $ % D y K
h t t p : / / w w w . . m h a n p . o r g / a d m i n y K
8 h t t p : / / w w w . . m h a n p . o r g / a d m i n
i 8 @ 8
N o r m a l CJ _H aJ mH sH tH < A@ <
D e f a u l t P a r a g r a p h F o n t . U@ .
H y p e r l i n k
>* B* ph h $ h
E n v e l o p e A d d r e s s ! @
﷓ &+D/ ^@
5 CJ OJ QJ ^J * + ) * - . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


e X k I M S W % Z ] j n J R / 3 3 3 3 3 3 O E M c C : \ D o c u m e n t s a n d S e t t i n g s \ A n d r e w \ A p p l i c a t i o n D a t a \ M i c r o s o f t \ W o r d \ A u t o R e c o v e r y s a v e o f D o c u m e n t 1 . a s d O E M A C : \ D o c u m e n t s a n d S e t t i n g s \ A n d r e w \ D e s k t o p \ A n d y \ h t a c c e s s \ R e a d M e . d o c O E M A C : \ D o c u m e n t s a n d S e t t i n g s \ A n d r e w \ D e s k t o p \ A n d y \ h t a c c e s s \ R e a d M e . d o c @ ` @ U n k n o w n G z T i m e s N e w R o m a n 5 S y m b o l 3&
z A r i a l " q h Erxrxr 3 <
20 2 @ 1 A f i l e s y s t e m h a s b e e n c r e a t e d t o s t o r e d o c u m e n t s O E M O E M Oh +'0
, 8
T
`
l
x
﷓ 2 A file system has been created to store documents d ﷓ fi﷓ OEM ﷓ EM ﷓ EM ﷓
Normal.dot e﷓ OEM ﷓ 2 M ﷓ Microsoft Word 9.0 e@ @ @ 9 @ <
՜. +,D ՜. +,\
h p |


﷓ /




﷓ 2 A file system has been created to store documents
﷓ Title 8 @
_PID_HLINKS A p ( } h t t p : / / w w w . . m h a n p . o r g / a d m i n



﷓ ! " $ % & ' ( ) * - R o o t E n t r y F \ / D a t a

1 T a b l e
W o r d D o c u m e n t & S u m m a r y I n f o r m a t i o n ( D o c u m e n t S u m m a r y I n f o r m a t i o n 8 # C o m p O b j j O b j e c t P o o l \ \
F Microsoft Word Document
MSWordDoc Word.Document.8 9q


This happens if I upload the document using PHP or if I ftp the document. Same result.
Any idea why this is returned?

Andy

Fou-Lu
02-08-2010, 09:54 PM
Definitely doc. Lets see, will fault lay with the upload or the download?
First thing I'd do, is transfer a file up via ftp, and then download that same file to a different location to ensure that its upload is successful. Then, the same but upload with you're upload form into the desired location and download it with ftp/ssh to view it again. If these prove alright, the fault will lay with the actual download.
Now, I'm fairly certain you don't want to use the octet/stream for a header. Application/msword or application/vnd.msword should suffice. But aside from that, I'm not sure what the problem is....


Oh wait, can you add a header for header("Content-Transfer-Encoding: binary");? See if that does the trick.

MattF
02-08-2010, 11:21 PM
header ("Content-type: octet/stream");
header ("Conent-type: application/vnd.ms-word");


Content, not conent. Also, ditch the octet/stream line. It's pointless.

1andyw
02-09-2010, 01:11 AM
header ("Content-type: octet/stream");
header ("Conent-type: application/vnd.ms-word");


Content, not conent. Also, ditch the octet/stream line. It's pointless.

I corrected the spelling for Content-type and deleted the octet/stream line.

I ftp the word document to my desktop and it opens correctly. I click the link on the web page, use the option to open and Word displays the same junk.


Edit:
Oh wait, can you add a header for header("Content-Transfer-Encoding: binary");? See if that does the trick.

Added that line and the dialog now lists firefox as the preferred program to open the .doc but that is the only difference. I use word to open the .doc and I get junk.

Since this problem didn't exist in PHP4, could PHP5 require something different in my code?

Andy

MattF
02-09-2010, 01:20 AM
Try adding the ob_clean and flush lines as shown in the example script:

http://uk2.php.net/manual/en/function.readfile.php

1andyw
02-09-2010, 02:27 AM
Matt,

I changed my existing code, "ob_end_flush();" to the example and the screen now is blank. I reverted to the above and am still stuck. I tried opening the file in Open Office 3 and it returned the same code mix as did Word.

MattF
02-09-2010, 02:36 AM
Matt,

I changed my existing code, "ob_end_flush();" to the example and the screen now is blank. I reverted to the above and am still stuck. I tried opening the file in Open Office 3 and it returned the same code mix as did Word.

Wrong part. :) Try exchanging either of these two blocks with the relevant portion of your code above and see if they make any difference. You may need to update that $fileLocation again, as I've practically copied from your code above.



if(isset($_GET['id']))
{ //this section handles file download
$fileLocation = '/../../../script/users/'.$_SESSION['username'].'/';
$fileName = $_GET['id'];
$file = ($fileLocation . $fileName);
header ("Content-type: application/vnd.ms-word");
header ("Content-disposition: attachment; filename=".$fileName.";");
header("Content-Length: ".filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}


or:



if(isset($_GET['id']))
{ //this section handles file download
$fileLocation = '/../../../script/users/'.$_SESSION['username'].'/';
$fileName = $_GET['id'];
$file = ($fileLocation . $fileName);
header ("Content-type: application/octet-stream");
header ("Content-disposition: attachment; filename=".$fileName.";");
header("Content-Length: ".filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

Fou-Lu
02-09-2010, 02:55 AM
I don't have office, so I'm afraid I cannot test it against it. What I can tell you is that this works fine for odt file:


header("Content-type: application/vnd.oasis.opendocument.text");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$fileName.";");
header("Content-Length: ".filesize($file));
readfile($file);


So that leads me to believe that the application/vnd.ms-word mimetype is invalid. See if you can push it through using application/msword or application/vnd.openxmlformats-officedocument.wordprocessingml.document if for docx files.

1andyw
02-09-2010, 03:22 AM
Problem is solved!

Matt, I pasted your first code into my file and that did the trick. Works now as it did before the upgrade. Great!

How does that ob_clean, flush make this happen?

Thanks folks,

Andy

MattF
02-09-2010, 03:42 AM
There was probably some content in the buffer which was being dumped along with the file contents. That code from the php site would flush any buffered output before the readfile output was sent.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum