2013-02-20 29 views
1

嗨,PHP新手們一直在嘗試使用代碼複製和粘貼(不要討厭我)並將其剪裁以適應我的需要,但我已經打了一堵牆。這樣做的問題是很難得到充分的理解。這是問題。我正在將一個文件從一個目錄複製到另一個文件中,並將其重命名。但是我想實際上從源文件複製一個文件(這很好),然後把它放在一個名爲users的文件夾中。嘗試了幾個不同的選擇,只是得到錯誤。任何幫助讚賞。如何在複製時設置目標文件夾PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>User registration</title> 
</head> 
<body> 
<?php 
require_once('config.php'); // include config file 
require_once('function.php'); // include copy file that have copy function. 
?> 
<div class="center"> 
<h1>Please Enter the details shown below..</h1> 
<form action="index.php?action=submit" method="post"> 
<table class="table" align="center"> 
<tr> 
<td>User Name:</td> 
<td><input type="text" name="uname" /></td> 
</tr> 
<tr> 
<td>Password:</td> 
<td><input type="password" name="pwd" /></td> 
</tr> 
<tr> 
<td>Click on Submit..</td> 
<td><input type="submit" value="Submit" name="action"/></td> 
</tr> 
</table> 
</form> 
</div> 
<?php 
if($_POST["action"]=='') // check for parameter if action=submit or not if it is blank then show this message 
{ 
echo "Please fill 'User Name' and 'Password' above!"; 
} 
else{ 
if($_POST["uname"]==''){ // if username left then check for password field 
if($_POST["pwd"]==''){ // if it also blank then show this message 
echo "You leave both the fields blank. Please fill them and then click on submit to continue."; 
} 
else {echo "User Name field can not be left blank."; // else show user name left blank 
} 
} 
elseif ($_POST["pwd"]==''){ // if username is there then check for a null password 
echo "Password field can not be left blank."; 
} 
else{ 
// We will add User into database here.. 
$query="INSERT INTO $DBTable (username, password) 
VALUES('$_POST[uname]','$_POST[pwd]')"; 
// We are doing it for an example. Please encrypt your password before using it on your website 
if (!mysql_query($query,$con)) 
{ 
die('Error: ' . mysql_error()); 
} 
echo "1 record added "; 

// Now Create a directory as User Name. 
$username=$_POST["uname"]; 
mkdir(dirname(__FILE__)."https://stackoverflow.com/users/"."$username"); // Create Directory 
recurse_copy($SourceDir,$username); // Copy files from source directory to target directory 
// Finally print the message shown below.?> 
    Welcome <?php echo $_POST["uname"]; ?>! 
    You are account folder with name <?php echo $_POST["uname"]; ?> has been created successfully. 
    <?}}?> 
    </body> 
    </html> 

這是函數代碼。

<?php 
// This source code is copied from http://php.net/manual/en/function.copy.php 
// Real author of this code is gimmicklessgpt at gmail dot com 
function recurse_copy($src,$dst) { // recursive function that copy files from one directory to another 
$dir = opendir($src); 
@mkdir($dst); 
while(false !== ($file = readdir($dir))) { 
    if (($file != '.') && ($file != '..')) { 
     if (is_dir($src . '/' . $file)) { 
      recurse_copy($src . '/' . $file,$dst . '/' . $file); 
     } 
     else { 
      copy($src . '/' . $file,$dst . '/' . $file); 
     } 
    } 
    } 
    closedir($dir); 
    //echo "$src"; 
} 

?> 

不要想這個配置文件包含任何會改變副本,以便不包括的目的地。

+0

如果您收到錯誤,您應該發佈它們,以便我們確切知道發生了什麼。 – Axel 2013-02-20 15:31:56

+0

獲取錯誤。請問什麼錯誤? – 2013-02-20 15:32:15

+0

在啓動任何html之前,將您的包含邏輯的文件包含爲配置文件。 – vikingmaster 2013-02-20 15:34:04

回答

0

我看到2個問題。第一個是你如何調用你的函數:

mkdir(dirname(__FILE__)."https://stackoverflow.com/users/"."$username"); // Create Directory 
recurse_copy($SourceDir,$username); // Copy files from source directory to target directory 

應該是:

// Create Directory 
mkdir(dirname(__FILE__)."https://stackoverflow.com/users/"."$username"); 
// Copy files from source directory to target directory; the one you just created 
recurse_copy($SourceDir,dirname(__FILE__)."https://stackoverflow.com/users/"."$username"); 

第二是在你的函數的@mkdir($dst);。您應該將您的邏輯更改爲僅創建原始目標的子目錄。

+1

這樣做了Jeroen。非常感謝! – BrogenMacy 2013-02-20 16:00:55

相關問題