2012-09-28 81 views
-1

我試圖創建一個相當簡單的(理論上)文件上傳'進程'。我瀏覽了這個網站,雖然有我需要的片段,但似乎沒有任何工作!文件上傳/ mkdir()問題

基本上,我試圖創建一個處理如下功能:

  1. 獲取文件擴展
  2. 獲取文件名
  3. 合併的文件名,clientID的和擴展
  4. 檢查目標目錄存在。如果沒有,創建它。
  5. 文件(在新名稱)移動到目錄

我的文件結構如下:

    • 管理
      • 上傳
        • client_uploads
          • $客戶端ID

這是我有:

<?php 
include("dbconnect.php"); 

error_reporting(-1); // ALL messages and 
ini_set('display_errors', 'On'); 

if($_GET['clientID']){ 

//This function separates the extension from the rest of the file name and returns it 
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

//Get filename function 
$filename = "test"; //I dont know how to create this function at the moment 
return $filename; 

//This applies the function to our file 
$ext = findexts ($_FILES['uploaded']['name']) ; 

// we will be using the clientID as the new folder and adding it into the filename 
$clientID = mysql_real_escape_string((int)$_GET['clientID']); 

//merge filename 
$filename2 = $filename."_".$clientID."."; 

//Scan for existing directory 
$folder = '../admin/uploads/client_uploads/'.$username.'/'; 
if (is_dir("$folder") == false) { 
mkdir("$folder", 0777);  
echo "Directory created"; 
} 


//This assigns the subdirectory you want to save into... make sure it exists! 
$target = "admin/client_uploads/$clientID"; 

//This combines the directory, the random file name, and the extension 
$target = $target . $filename2.$ext; 

//Move file under new name 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file has been uploaded as ".$filename2.$ext; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
?> 

我沒有收到任何錯誤消息(儘管我已經明確要求他們),我最終只是一個空白的屏幕。文件夾不是在我的服務器上的任何地方創建的,沒有任何反應

任何想法?提前致謝。

+1

你是否檢查過PHP的錯誤日誌中的消息?你是否試圖通過註釋掉部分和'var_dump'個別變量來逐步調試? – deceze

+0

我不知道如何檢查我的錯誤日誌 - 我會以爲我的第一行代碼會在頁面上顯示任何錯誤? – Anthony

+0

似乎也是'分裂'命令的錯誤 - 關於它的折舊。我需要使用'爆炸'cammond還是其他地方? – Anthony

回答

0

在雙引號中使用已使用的$文件夾。刪除和使用

//Scan for existing directory 
$folder = '../admin/uploads/client_uploads/'.$username.'/'; 
if (is_dir($folder) == false) { 
mkdir($folder, 0777);  
echo "Directory created"; 
} 
+0

試過這個,仍然沒有。根據我上面的代碼,甚至將變量$ username更改爲$ clientID。 – Anthony

+0

請重試此代碼 –

0

我認爲你必須設置在服務器中寫權限......這是需要逐個創建目錄...試試這個....

$parts = explode("/",$folder); 
for($j=0; $j<count($parts); $j++) 
{ 
    $fpath = ""; 
    for($k=0; $k<=$j; $k++) 
    { 
    $fpath .= $parts[$k]."/"; 
    } 
    if(!is_dir($fpath)) 
    { 
    $oldmask=umask(0); 
    mkdir($fpath,0755); 
    umask($oldmask); 
    } 
} 
+0

再次,沒有任何返回。沒有創建目錄。根本沒有迴應。當然,創建這樣一個簡單的功能是不容易的......可以嗎? – Anthony

+0

發表另一個。嘗試一下 –

1

試試這個,

<?php 
//get file name in your own way 
$filename = $_FILES["filename"]["name"]; 
$fLength = strlen($filename); 
$exParts = explode(".",$filename); 
$totalParts = count($exParts); 
$extension = $exParts[$totalParts-1]; 
$eLength = strlen($extension); 
$filename = substr($filename,0,($fLength-$eLength+1)); 
//Get the client ID as you need 
$client_ID = //client ID here; 
$up_file_name = $filename."_".$client_ID.".".$extension; 
$folder  = $_SERVER['DOCUMENT_ROOT']."/admin/uploads/client_uploads/".$username."/"; 

if(!is_dir($folder)) 
{ 
//Create directory 
$parts = explode("/",$folder); 
for($j=0; $j<count($parts); $j++) 
{ 
$fpath = ""; 
for($k=0; $k<=$j; $k++) 
{ 
$fpath .= $parts[$k]."/"; 
} 
if(!is_dir($fpath)) 
{ 
$oldmask=umask(0); 
mkdir($fpath,0755); 
umask($oldmask); 
} 
} 
} 
//Upload your file 
$pathtoupload = $folder.$up_file_name; 
if(move_uploaded_file($_FILES["filename"]["tmp_name"],$pathtoupload)) 
{ 
echo "Uploaded successfully"; 
} 
else 
{ 
echo "Can't upload"; 
} 
?>