Page 1 of 1

[solved]how to make file copy in php

Posted: Mon Jul 30, 2007 11:36 am
by dharprog
Hi

I have tried so many times to work it out. But i am unable to do it.
I have tried to copy a file from one directory to another directory but its working in my local computer but after uploading this to the godaddy server its not working for me.

Code given below.

Code: Select all

if($work=="add_site")
        {
		  $short = $_POST['short_name'];
		  if(mkdir("../$short/", 0777))
			{ 
			$file = "../includes/index.php";
            $newfile = "../$short/index.php";
			
			if(copy($file, $newfile)=='1'){
		  $str = "insert into table_site(short_name,display_name,tot_lines,block) values('".$_POST['short_name']."','".$_POST['display_name']."','".$_POST['tot_lines']."',0)";
			  $str = mysql_query($str);
	 
		  if(mysql_affected_rows()>=1)
		     {
			   $_SESSION['msg'] = "Successfully site added";
				   header("Location:admin_sites.php"); 
			  }
				   }
				   else 
				   {
				   $_SESSION['msg'] = "Failed to copy file";
				   header("Location:add_site.php"); 
				   }
			 }
		}
Please let me know if anybody has come across this problem and got the solution.

Thank you very much.

Posted: Mon Jul 30, 2007 11:42 am
by onion2k
Slightly off-topic, but..

Code: Select all

$short = $_POST['short_name'];
                  if(mkdir("../$short/", 0777))
DO NOT DO THAT. That's a really, really massive security issue.

Posted: Mon Jul 30, 2007 11:46 am
by dharprog
Hi

Thanks for your reply but could you please tell me is there any other way to do it?

Thank you.

Posted: Mon Jul 30, 2007 12:02 pm
by superdezign
If it works on your computer and not the server, you have issues with file / folder permissions.

dharprog wrote:Thanks for your reply but could you please tell me is there any other way to do it?
Filter and validate ALL user input. ALL of it.

Posted: Mon Jul 30, 2007 12:08 pm
by dharprog
Hi

I tried with setting different permissions like 777 , 755, 646 etc... but not working.

Folder gets creating except file copying only not working.

I set the 777 so all the permissions are allowed like read, write and execution.

Even though write permission set file copying is not working for the above script.

All the input is correct.

Thank you.

Posted: Mon Jul 30, 2007 12:14 pm
by Benjamin
Turn on your error reporting and display errors or view the error log. Also might want into setting a parameter in mkdir to true, which will create full paths if they don't exist.

Posted: Mon Jul 30, 2007 12:29 pm
by dharprog
Hi

I dont know how to turn on error reporting. Could you please tell me and where to do this?

I'm getting upto creating a directory if not exists, but i'm getting problem at file copying to that directory.

Thank you.

Posted: Mon Jul 30, 2007 1:18 pm
by Benjamin
echo $file and $newfile and check to see if $file exists using file_exists(); and is_file();

You'll need to research how to turn on error reporting and display errors. Are these both linux computers?

Posted: Mon Jul 30, 2007 9:19 pm
by dharprog
Hi

My local computer is windows xp and using wamp server.But server is linux.

Thanking you.

Posted: Thu Aug 02, 2007 11:28 pm
by dharprog
Hi

what i came to know is when make a directory in server through php programme with permissions setting 777, i could see that the user for the folder is 'apache', if i make a folder through ftp or in the server itself the the user of the folder is 'ftp user id'.

What is this i don't understand. Please if anybody know then tell me, May be the solution would be here.

Could you please tell me how to handle Hosting Server side directories and files with file operations?

Is there any best tutorial for that?

Thank you.

Posted: Fri Aug 03, 2007 12:14 am
by iknownothing
back to the original topic..

Code: Select all

if(copy($file, $newfile)=='1'){
is that valid, or should it be..

Code: Select all

if(copy($file, $newfile)==TRUE){

Posted: Fri Aug 03, 2007 12:25 am
by dharprog
Hi

If you dont mind one thing i would like to remind you that:

if it doesnt work then how can i say/post here that its working in local computer?

And now i tested and that its giving the return value as 1. And that to its working for TRUE also.

Thank you very much.