Forum Moderators: coopster

Message Too Old, No Replies

File doesn't show up on browser when created

         

jm21

8:06 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Hi, I have a problem with file owner/group permissions...i think. I have a form which when submitted a script generates a word doc and stores it in a folder on the server. The script is simple and works perfectly on the local server however when i upload it to the work server the file is created but cannot be accessed, downloaded or viewed on a browser. I can view the file on ftp but cant edit/delete it or view it on a browser! Anyone with an idea whats happening?

here's my code

$fp = fopen("$file_name ","a+");
if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
}

dreamcatcher

9:00 am on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jm21,

It could be a permissions issue, so try the chmod [us.php.net] function after writing the file:


fclose($fp); // Close the file
@chmod($csvData,0644);

dc

jm21

9:12 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Hi, I've tried that...tried chmoding and chgrping!

The weird thing is that if i try to chmod or chgrp just after the file has been created it returns an error saying the file can't be found even when the file exists and the path is correct. I did a recursive chmod on the directory...changed everything to 0777 however the problem still remains...a web or ftp user cannot view/edit/delete the generated file even under 777 permissions...

penders

12:37 pm on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...a web or ftp user cannot view/edit/delete the generated file even under 777 permissions...

If the file did have 0777 perms then everyone whould be able to view the file - so it looks like your recursive chmod failed on this file. An FTP user is unlikely to have enough perms by default, unless your PHP script has changed them. The FTP user is usually different to the user that PHP runs under.

Have you tried clearing the umask before creating your file? Something like:

$old_umask = umask(0); 
$fp = fopen($file_name,'wb');
umask($old_umask);

jm21

1:35 pm on Aug 5, 2009 (gmt 0)

10+ Year Member



just figured out what the problem was....when i created the file there was a space in fopen after the file name...therefore the system creates the word document and appends a space at the end...something like "test.doc " instead of "test.doc" , so in the browser the file wont be found!

The morale of the story....be very careful when creating files in a Linux/Unix environment

$fp = fopen("$file_name ","a+");
if($fp){

penders

2:23 pm on Aug 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it sorted! :)

Small tip... don't enclose your variable name in double quotes, you are making PHP do more work.
ie.

$fp = fopen($file_name,"a+");