-1

かなり単純な (理論上) ファイル アップロード 'プロセス' を作成しようとしています。サイトをくまなく調べましたが、必要なもののスニペットはありますが、何も機能していないようです!

基本的に、私は以下のように処理する関数を作成しようとしています:

  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.";
 }
}
?>

エラー メッセージは表示されず (明示的に要求しましたが)、空白の画面が表示されます。サーバーのどこにもフォルダーが作成されず、何も起こりません。

何か案は?前もって感謝します。

4

3 に答える 3

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";
}
?>
于 2012-09-28T08:02:08.580 に答える
0

サーバーに書き込み権限を設定する必要があると思います...そして、ディレクトリを1つずつ作成する必要があります...これを試してください...。

$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);
  }
}
于 2012-09-28T06:52:08.453 に答える
0

used $folder を二重引用符で囲んで使用します。それを取り除いて使う

 //Scan for existing directory
 $folder = '../admin/uploads/client_uploads/'.$username.'/';
 if (is_dir($folder) == false) {
 mkdir($folder, 0777);    
 echo "Directory created";
 } 
于 2012-09-28T06:32:07.743 に答える