2012-09-28 7 views
-1

상당히 간단한 (이론적으로) 파일 업로드 '프로세스'를 만들려고합니다. 나는 사이트를 빠져 나갔고, 내가 필요로하는 부분이 있지만 아무것도 작동하지 않는 것 같다!파일 업로드/mkdir() 문제

  1. 가져 오기 파일 확장자
  2. 가져 오기 파일 이름
  3. 결합 파일 이름, 클라이언트 ID 및 확장
  4. 확인 대상이 그 :

    기본적으로, 다음과 같이 처리하는 함수를 작성하는 것을 시도하고있다 디렉토리가 존재합니다. 그렇지 않다면 생성하십시오.

    • 루트
      • 관리자
        • 업로드
          • client_uploads을 :
          • 다음과 같이

내 파일 구조가있는 디렉토리로 (새로운 이름 아래) 파일을 이동

    • 0 내가 명시 적으로 요청했습니다 있지만

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

      내가 (오류 메시지를받지 못했습니다 :

    • $ 여기

  • 내가 가진 무엇을 된 ClientID 그들) 그리고 나는 단지 빈 화면으로 끝난다. 폴더가 내 서버의 아무 곳에도 생성되지 않으며 아무 것도 발생하지 않습니다.

    아이디어가 있으십니까? 미리 감사드립니다.

    +1

    PHP 오류 로그에서 메시지를 확인 했습니까? 섹션을 주석 처리하고 개별 변수를 'var_dump'하여 단계별로 디버깅하려 했습니까? – deceze

    +0

    내 오류 로그를 확인하는 방법을 모릅니다. 코드의 첫 번째 줄마다 페이지에 오류가 표시되었을 것입니다. – Anthony

    +0

    'split'명령에 오류가있는 것처럼 보입니다. 나는 그것의 장소에있는 '폭발하는'카 몬드 또는 어떤 것을 사용해야합니까? – 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"; 
    } 
    ?>