Log in

View Full Version : PHP copy() function



stu2000
01-19-2009, 05:56 PM
Hi there,

I'm having trouble with PHP's copy() function, and I have no idea what the solution is, any help muchly appreciated!

I'm trying to copy a .php file from one directory to another directory. However when I copy the .php file, it copies fine, BUT when I run the file, it just plain skips the PHP coding within the file, as if it's // taged out... When I copy the file through my FTP client manually, it works fine. I have NO idea why it's doing this!

Anyone have any ideas?

Thanks,

Stu

Twey
01-19-2009, 06:45 PM
Make sure the execute bits are set. Is the PHP code visible if you view the page source?

stu2000
01-19-2009, 07:20 PM
Hi Twey,

Thanks for your reply, I've just got a quick question:

Execute bits?

... and as for the PHP running, even a simple
include 'footer.php'; line isn't running, so no footer is being shown, among other missed bits which all work fine if I upload the file through my FTP client manually.

Thanks

Stu

p.s.

Oh and also, if I re-upload the file (as I have a copy on my harddrive), and overwrite the file on the server that the PHP copy() function created, the file still doesn't work... which is odd, I'm guessing it might be some kind of permission problem? In the PHP admin file that's doing the copying, I used chmod() to change the file permission to 777 right after I copied it, if that makes any difference?

Twey
01-19-2009, 08:14 PM
Of course the footer isn't being shown, but is there a <?php include 'footer.php; ?> in the page source somewhere? The surrounding <> will render it an unknown tag to the browser, so it probably won't be displayed.

stu2000
01-19-2009, 08:22 PM
Hi Twey,

yeah, two of the lines within the page are


<?php include 'header.php'; ?>

and


<?php include 'footer.php'; ?>

But there are other calls to the database etc that are also totally ignored.

How do you mean that the surrounding <>'s will render it an unknown tag to the browser?

Thanks,

Stu

stu2000
01-20-2009, 09:44 AM
When you say 'in the page source', do you mean the output that can be see by clicking 'view source'? As that's a no, the code isn't:


&lt;?php include 'footer.php' ?&gt;

it's still how it should be, i.e:


<?php include 'footer.php'; ?>

and instead of outputting the footer, the page source is just:


<html>
<body>
</body>
</html>

Stu

Schmoopy
01-20-2009, 10:44 AM
&lt;?php include 'footer.php' ?&gt;


That doesn't look good, if PHP is enabled - it shouldn't be showing up in the page source, it should be completely hidden, are you sure the file is saved as .php and that your host has PHP enabled?

stu2000
01-20-2009, 10:48 AM
no no, I mean the code isn't


&lt;?php include 'footer.php' ?&gt;

it displays nothing in the source, and if I download the file and open it in an HTML editor, it looks fine, as in:


<?php include 'footer.php' ?>

Stu

Schmoopy
01-20-2009, 10:55 AM
I see, any chance you could link to the page, and are you sure PHP is enabled in the server you are using?

stu2000
01-20-2009, 11:19 AM
yeah, no problem,

a couple of examples here, the first one, is the file copied by the PHP copy() function (which is now displaying absolutely nothing! I just got the background image before and nothing else):

http://www.lynchfordaquatics.co.uk/newsite/products/testprod1.php

and this next one is the same file, I downloaded, renamed and uploaded through my FTP client:

http://www.lynchfordaquatics.co.uk/newsite/products/testprod2.php

As you can see, this one displays everything as it should do, even though it is exactly the same file.

Thanks Schmoopy :)

p.s.

If the actual code of the php file would help you, let me know and I can post it for you, thanks for your help!

djr33
01-20-2009, 12:21 PM
I think the issue is back to what Twey said originally:
1. When you are copying it the CHMOD permissions are disabling it's execution (make it '777').
2. Perhaps the actual data is not being transferred properly-- so be sure the text always shows up properly in the file once copied, and perhaps check things like character encoding if that for some odd reason might cause a problem.

The other test here is to copy a file that is not included, then run that independently. If that runs, but the included version does not, then the inclusion is the issue, not the copying. Of course if it does copy and then doesn't run it's the copying that is a problem.

I hope this points you in the right direction. Sounds like a strange problem.

Also, having used the copy function a couple times, I found it useful to look at the comments and code suggestions below it on the php.net page, and there are a few things (like copying a directory recursively) that are helpful and might help with your problem, such as showing you how to copy with correct permissions (assuming that's an issue, someone else must have run into it before).

stu2000
01-20-2009, 12:57 PM
Hi djr33,

Thanks for the help, the code I'm using is:


$source_file = '../products/template.php';
$dest_file = '../products/' . @$_POST['product_code'] . '.php';
copy($source_file,$dest_file);
chmod ($dest_file, 0777);

The chmod seems to work fine and the file attributes show up in my FTP client as:

-rwxrwxrwx (which is 777 as far as I'm aware?)

As for the code inside the actual file, that is 100% in tact. I've downloaded the copied file (/products/testprod1.php in this case) and it's contents are exactly the same as the template file which it is a copy of.

I'll have a look at character encoding and also take a look at the php.net site to see if that has any answers, thanks for the suggestions!

On an aside, another related problem, I *was* trying to create a directory for the products ( ie. www.mydomain.com/products/testprod1/) and store all images and the actual product info in there, I can create the directory fine using:


mkdir("../products/" . $product_code, 0777);

However, I can't copy files into it (howeverI can save image files through a file upload into this newly created directory)... which leads me to believe both these problems are some kind of permission problem?

Ta,

Stu

stu2000
01-20-2009, 05:06 PM
ok, I've just gone through line by line and debugged the file, and it's just the 'include' functions that aren't working.

Does anyone have any idea why copying a file using the PHP copy() function would case the file to just plain skip over any lines of code that use the include function?

The reason absolutely nothing was displaying was because the way I was handling a mysql error. There is a line of:


include 'config.php';

which wasn't being 'included', and this was obviously causing any sql queries not to run.

Anyone have any ideas why no includes at all are running when this php file is copied? But all other php commands work fine?!

*pulls hair out!*

Twey, you said originally,


Of course the footer isn't being shown

Is there something I'm totally missing?

Thanks

Stu

djr33
01-20-2009, 05:18 PM
So it's the include functions within the copied page, or including those copied pages, or both?

Also, does it work on a reload-- perhaps for some reason it only fails in the same execution in which it is created. That might give more information.

I agree that it seems it has to do with a permission error, but I'm not sure what (include and copy are allowed individually, but not together?), which leads me to think it may be a bug, not a lack of permission.

stu2000
01-20-2009, 05:24 PM
It's the 'include' functions within the copied page that are not working at all, it's just skipping over them, so everything that should be 'included' inside the copied file, isn't!

The pages that are meant to be included are fine. I've tried reloading the copied page, but that doesn't help. Downloading it, deleting it off the server, and then uploading it does, however, make it work.

I hope I've explained that ok!

Thanks,

Stu

stu2000
01-20-2009, 08:56 PM
ok, I'm getting there... slowly! The problem is a permission problem, how do I get round the following error:


Warning: main() [function.main]: SAFE MODE Restriction in effect. The script whose uid is 48 is not allowed to access ../admin/config.php owned by uid 10024 in /var/www/vhosts/lynchfordaquatics.co.uk/httpdocs/newsite/products/testprod3.php on line 7

Warning: main(../admin/config.php) [function.main]: failed to open stream: No such file or directory in /var/www/vhosts/lynchfordaquatics.co.uk/httpdocs/newsite/products/testprod3.php on line 7

Warning: main() [function.include]: Failed opening '../admin/config.php' for inclusion (include_path='.:') in /var/www/vhosts/lynchfordaquatics.co.uk/httpdocs/newsite/products/testprod3.php on line 7

Why oh why doesn't it have permission to access the other config file?!

Stu

Twey
01-20-2009, 09:48 PM
Because they are owned by different users. Probably your files have a special 'web user', named 'nobody' or 'www-data'. You need to chown the files to the user who owns the rest of your files.