Jump to content

Copying Files with the copy( ) Function


iwato

Recommended Posts

QUESTION: Is it a problem of path, file name, or something completely different all together? The following code fails, and I do not know why.

			<?php				$file = "../fopen/nonPHP_file.txt";				$newfile = "nonPHP_file.txt";				if (!copy($file, $newfile)) {				    echo "Failed to copy " . basename($file) . ".";				}			?>

BACKGROUND: The PHP file in which this code resides is located in the same folder into which $file is to be copied. Also, $newfile already exits with the same indicated name in the folder into which $file is to be copied.ERROR MESSAGE: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRINGRoddy

Link to comment
Share on other sites

You can use file_exists to test if the file exists if you're not sure about that. But a parse error means the code is not structured correctly, it doesn't have anything to do with the logic. I don't see an error in that code though.

Link to comment
Share on other sites

That's not all of your code, is it? Parse errors can often give misleading line numbers, so the only way for us to know why the error occurs is if you provide the full source of your file.But anyway, here's an easy way to pinpoint parsing errors - use an IDE that can highlight PHP and give you errors within it, such as NetBeans. The reason for the error is often (if not always) before the error, right after the highlighting starts to feel out of place.

Link to comment
Share on other sites

You can use file_exists to test if the file exists if you're not sure about that. But a parse error means the code is not structured correctly, it doesn't have anything to do with the logic. I don't see an error in that code though.
Thank you for replying.If I remember correctly the copy() function is suppose to create the destination file if it does not already exist. What I have learned since I began using the copy() function is that my administrator's write permissions are automatically suppressed when I create a folder using Dreamweaver in the MAMP webserver environment -- this, despite the fact that as the computer's user I am allowed both read and write permissions.My computer has two accounts that share administrative rights. Could this be a source of my difficulty?
Link to comment
Share on other sites

That's not all of your code, is it? Parse errors can often give misleading line numbers, so the only way for us to know why the error occurs is if you provide the full source of your file.
Yes, it is all of my code. I tend to work in increments so as to avoid creating enormous puzzles that I cannot unravel. As you can see, however, even this precaution is not fail safe.
But anyway, here's an easy way to pinpoint parsing errors - use an IDE that can highlight PHP and give you errors within it, such as NetBeans. The reason for the error is often (if not always) before the error, right after the highlighting starts to feel out of place.
I have explored the NetBeans webpage and bookmarked it. Although I can well appreciate what you are recommending, I am still a novice when it comes to web application development and fear that NetBeans and my newly acquired and installed MAMP Pro software are likely to come into conflict. So, I must wait until I have better understood the problems associated with my current developing environment. My current technology may be inadequate, but starting all over would render me hopefully confused, I fear.Roddy
Link to comment
Share on other sites

NetBeans is only an editor. It's not a web server package. It will NOT come in conflict with MAMP Pro.I suspected this is not all of your code for two reasons:1. You haven't showed the line number. That line number is crutial in pinpointing the error.2. Your PHP code is indended from the very start very much. As if there's something surrounding it, say, (X)HTML code for example.[edit]Yeah... we also already said we can't see any syntax errors in the snippet[/edit]

Link to comment
Share on other sites

NetBeans is only an editor. It's not a web server package. It will NOT come in conflict with MAMP Pro.
This is, of course, good news, but it would still be a very big step, as I have just spent the past year getting use to Dreamweaver after having been informed by Adobe that GoLive would not undergo further development.
I suspected this is not all of your code for two reasons:1. You haven't showed the line number. That line number is crutial in pinpointing the error.2. Your PHP code is indended from the very start very much. As if there's something surrounding it, say, (X)HTML code for example.
Well, yes. The file is replete with HTML code, but it is only a laboratory framework for unfamiliar PHP code that I test. Other PHP code has been housed in the same HTML framework without consequence.Roddy
Link to comment
Share on other sites

This is, of course, good news, but it would still be a very big step, as I have just spent the past year getting use to Dreamweaver after having been informed by Adobe that GoLive would not undergo further development.
You can always have both editors installed, and only use NetBeans for cases like that.Dreamwaver also has syntax highlighting of PHP code, which is a good start. Don't you see a place in the code before the error where the highligting seems odd?
Well, yes. The file is replete with HTML code, but it is only a laboratory framework for unfamiliar PHP code that I test. Other PHP code has been housed in the same HTML framework without consequence.Roddy
Does it by any chance start with
<?xml version="1.0" encoding="UTF-8"?>

or something similar? If so, this could mean that short tags are enabled in MAMP, and they cause the problems you're having.In your php.ini (I have no idea where MAMP would put it... see if you can find the "configuration file" in phpinfo()), make sure the setting short_open_tag is "Off" or "0".

Link to comment
Share on other sites

You can always have both editors installed, and only use NetBeans for cases like that.Dreamwaver also has syntax highlighting of PHP code, which is a good start. Don't you see a place in the code before the error where the highligting seems odd?
You really like NetBeans. Is it easy to get started? I am already awash in getting started with PHP.No, there is no strange or unusual highlighting in this file, although I have discovered yellow mark-up warnings in other files that I could not explain.
Does it by any chance start with
<?xml version="1.0" encoding="UTF-8"?>

or something similar? If so, this could mean that short tags are enabled in MAMP, and they cause the problems you're having.

No, I am running XHTML Strict, and it begins with the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

In your php.ini (I have no idea where MAMP would put it... see if you can find the "configuration file" in phpinfo()), make sure the setting short_open_tag is "Off" or "0".
I turned the short_open_tag to Off. Judging from the PHP website explanation use of the the short tag will surely cause me more trouble than its shorthand could ever provide benefit. Of course, I am not at all sure this change will fix my problem, but that's all right, as the road ahead of me is long, and I want to get off to the best start possible.Roddy
Link to comment
Share on other sites

What line does it say the parse error is on?
It shows up on the line after the final PHP tag ?>. The line contains a pair of HTML paragraph tags. Bizarre!Roddy
Link to comment
Share on other sites

Can you post the complete code?
The problem appears to have resolved itself when the permissions were changed. I can now copy files with the copy() function as prescribed by the function's specifications. Simply there was a delay in Dreamweaver's updating mechanism, and I did not see the file appear until later.Hooray!One small step for neophyte PHP user, but not yet a giant leap for human kind.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...