2014-02-13 53 views
0

當用戶註冊到我的網站時,我想用它們的用戶名和默認配置文件圖片圖像創建一個文件夾。我知道如何製作一個文件夾,但是如何製作一個包含文件的文件夾。如何用其中的圖像創建一個文件夾PHP

的文件夾應該是這個樣子:

/users/pcoulson/ 

(pcoulson將用戶的用戶名)

../pcoulson/應該有它的默認配置文件PIC是這樣的:

/users/pcoulson/default-profile_pic.png 

我該怎麼做PHP

+0

其中default-profile_pic.png來自哪裏?你只問如何複製文件..? – AD7six

回答

2
$dir='users/'.$username; 
mkdir($dir); 
copy('default-profile_pic.png',$dir.'/default-profile_pic.png' 
0
if(isset($_POST['add-user-submit']) && isset($_FILES['image']['name'])) 
{ 
    #label the form inputs 
    $username = $_POST['username']; 
    $image = $_FILES["image"]["name"]; // The file name 
    $fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder 
    $fileType = $_FILES["image"]["type"]; // The type of file it is 
    $fileSize = $_FILES["image"]["size"]; // File size in bytes 
    $fileErrorMsg = $_FILES["image"]["error"]; // 0 = false | 1 = true 
    $kaboom = explode(".", $eventFlyer); // Split file name into an array using the dot 
    $fileExt = end($kaboom); // Now target the last array element to get the file extension 

    if(!$fileTmpLoc) 
    { 
     $error = "Please insert ALL fields"; 
    } 
    elseif($fileSize > 2097152) 
    { 
     $error = "ERROR: Your file was larger than 2 Megabytes in size."; 
     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
    } 
    elseif(!preg_match("/.(gif|jpg|png)$/i", $image)) 
    { 
     $error = "ERROR: Your image was not .gif, .jpg, or .png."; 
     unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
    } 
    else if ($fileErrorMsg == 1) 
    { 
     $error = "ERROR: An error occured while processing the file. Try again."; 
    } 
    else 
    { 

     # move the file to a folder 
     $moveResult = move_uploaded_file($fileTmpLoc, "you file directory ex(../img/users/$username)"); 

     if($moveResult != true) // there was an error uploading the file to the folder 
     { 
      $error = "ERROR: File not uploaded. Try again."; 
      unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 
     } 
+0

哇,這是一個令人印象深刻的答案 – nitrous

+0

@ user3112869謝謝我剛剛寫了一個類似的腳本,所以我已經在我的頭腦 – user3051232

0

我建議將圖像保存在備份存儲,你的數據庫。

這樣做,圖像與用戶「強烈關聯」。一旦用戶名更改,它就不會變得不相關。

+0

你的意思是blob? – 2014-02-13 23:07:50

+0

@穆罕默德絕對!使用mySQL,您可能需要使用MEDIUMBLOB或MEDIUMBTEXT類型。 – SteAp

+0

但db中的文件路徑絕對是要走的路 – 2014-02-13 23:15:11

0
  1. 你首先要創建文件夾
  2. 複製圖片新genereated文件夾。

假設您退出用戶數據爲$userdata變量。您可以製作這樣的文件夾 而且我認爲您的default_picusers位於同一個目錄中。

$new_directory = 'users/'.$userdata['username']; 
mkdir($new_directory,0777); 
copy('default-profile_pic.png',$new_directory.'/default-profile_pic.png'); 
相關問題