File manipulation with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kenty
Forum Newbie
Posts: 3
Joined: Thu Oct 24, 2002 5:58 pm

File manipulation with PHP

Post by kenty »

I'm trying to do something relatively simple(at least it should be!). I want to take a file that is below the web directory(not accessible) and copy it up into the web directory so that it can be accessed. This needs to be called inline in a php script so that I can copy the file - access the file in an active x script - then delete the file. I'm at then end of my rope and a newbie to boot! Based on the reading I've done this should work but it keeps failing. My only thought is that it's looking in a different path than I think it is - any help would be greatly appreciated.

Code: Select all

<?

$orig_filename = "test.html";


$success = @copy($orig_filename, $orig_filename.'.bak') or die("Couldn't copy file.");

if ($success) &#123;
	$msg =  "Copied $orig_filename to $original_filename.'bak'";
&#125; else &#123;
	$msg =  "Could not copy file.";
&#125;

?>

<HTML>
<HEAD>
<TITLE>Copy a File</TITLE>
</HEAD>
<BODY>

<? echo "$msg"; ?>

</BODY>
</HTML>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

change

Code: Select all

$success = @copy($orig_filename, $orig_filename.'.bak') or die("Couldn't copy file.");
to

Code: Select all

$success = copy($orig_filename, $orig_filename.'.bak');
and see what PHP has to say.
kenty
Forum Newbie
Posts: 3
Joined: Thu Oct 24, 2002 5:58 pm

thanks for the tip

Post by kenty »

The error is this:

Warning: Unable to open '/home/catusr/test.html' for reading: No such file or directory in /home/webusr/html/kent_test/copy.php on line 6
Could not copy file

so basically I need to figure out how to make it start in the root directory when it goes to look for the file. Is that php.ini setting?

Thanks in advance for the help!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try using the full path.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

path name

Post by phpScott »

you can run a little script like this

Code: Select all

&lt;?php
   $root = getenv("DOCUMENT_ROOT"); 
   echo "root: ",$root,"&lt;br&gt;\n";
   
?&gt;
that will return some thing like:
root: /home/sites/site234/web
then rip of the /web or /public_html or the like.
then create a seperate file that defines a root variable

Code: Select all

&lt;?php
$ROOT='/home/sites/site234';
?&gt;
and then just include this file where ever needed.
your include path is hopefully outside the web directory and that would be a great place to store.
kenty
Forum Newbie
Posts: 3
Joined: Thu Oct 24, 2002 5:58 pm

Thanks for the help everyone!

Post by kenty »

With the debugging tips I was able to trace the problem to my linux file permissions(DUH!! :oops: )
I learned a lot in the meantime, so at least that's cool!
Thanks again for taking the time to help me out.


Kent
Post Reply