Jump to content

Creating Dir + copying/moving file


josephman1988

Recommended Posts

... in one form.

 

Hi guys,

 

On submission of the form, it creates a directory named by the title that is inputted.

Everything works fine, but upon submission i want to copy a file from 1 directory and copy it to the newly created directory.

 

Here was my attempt:

			$titlereplace = $_SERVER['DOCUMENT_ROOT'].'/';
		$titlereplace .= str_replace(' ', '-', $gTitle);	
		$titlereplace .= '/';
		$old = umask(0);
		$makedir = mkdir($titlereplace, 0777);
		umask($old); 
		$file = $_SERVER['DOCUMENT_ROOT'] . '/includes/details.php';
		$dest = $titlereplace;  
		if (!copy($file, $dest))
			{ 
			print 'Did not copy'; 
			}   

 

However the basic error of 'directory doesn't exist' occurs, and I don't know how to fix it.

 

Thanks for any help =]

Link to comment
https://forums.phpfreaks.com/topic/121550-creating-dir-copyingmoving-file/
Share on other sites

You are trying to write to the /includes directory off the doc root, so you need to be sure that directory exists before you write to it:

<?php
$dir = $_SERVER['DOCUMENT_ROOT'] . '/includes/';
if (!file_exists($dir))
{
  if (FALSE === mkdir($dir, 0775))
  {
    die('Could not create directory!');
  }
}

// Directory is now created, so move your file
?>

Im not trying to write to the includes am I? Im trying to pull the file from the includes to the newly created directory .. or am I clueless?

 

Would this be right or don't I get it?

			$titlereplace = $_SERVER['DOCUMENT_ROOT'].'/';
		$titlereplace .= str_replace(' ', '-', $gTitle);	
		$titlereplace .= '/';
		$old = umask(0);
		$makedir = mkdir($titlereplace, 0777);
		umask($old); 

		$file = $_SERVER['DOCUMENT_ROOT'] . '/includes/details.php';
		if (!file_exists($file))
		{
		  if (FALSE === mkdir($file, 0775))
		  {
			die('Could not create directory!');
		  }
		}

		copy($file, $titlereplace);   

 

-- PS: Yay at your Vivi avatar xD --

My bad. I took a cursory glance at it, and I misinterpreted what it was you are doing. I would just do the same check on the $titlereplace location before you try to write it. Also, the user executing the script has to have write access to the directory on the server. Check the location to which you are writing the directory as well for user permissions.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.