文件上传/ mkdir()问题

时间:2012-09-28 05:50:57

标签: php file file-upload upload mkdir

我正在尝试创建一个相当简单(理论上)的文件上传'流程'。我已经浏览了整个网站,虽然有我需要的片段,但似乎没有任何效果!

基本上,我正在尝试创建一个如下处理的函数:

  1. 获取文件扩展名
  2. 获取文件名
  3. 合并文件名,clientID和扩展名
  4. 检查目标目录是否存在。如果没有,请创建它。
  5. 将文件(以新名称)移动到目录
  6. 我的文件结构如下:

      • 管理员
        • 上传
          • client_uploads
            • $ clientID的

    这就是我所拥有的:

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

    我没有收到任何错误消息(虽然我已经明确要求了)但我最终得到了一个空白屏幕。文件夹不会在我的服务器上的任何位置创建,也没有任何反应。

    有什么想法吗?提前谢谢。

3 个答案:

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

答案 1 :(得分:0)

在doublequotes中使用used $ folder。删除它并使用

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

答案 2 :(得分: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);
  }
}