2014-01-10 64 views
6

我一直在尋找解決方案,但找不到答案。move_uploaded_file()無法將文件從tmp移動到目錄

我創建了一個圖片上傳表單。它使用ajaxform插件運行。但它仍然沒有上傳到目錄。 error_log says

move_uploaded_file()無法將文件從[tmp]移動到[dir]。

然後在前端說上傳完成。但是當文件被調用時,它不存在。

HTML代碼:

<div class="imgupload hidden"> 
    <form id="profpicform" action="" method="post" enctype="multipart/form-data"> 
     <input type="file" size="60" name="profpic"> 
     <input type="submit" value="Submit File"> 
    </form> 

    <div class="imguploadStatus"> 
     <div class="imguploadProgress"> 
      <div class="imguploadProgressBar"></div> 
      <div class="imguploadProgressPercent">0%</div> 
     </div> 
     <div class="imguploadMsg"></div> 
    </div> 
    <div class="imgpreview"></div> 
</div> 

JS:

var options = { 
    beforeSend: function() 
    { 
     $(".imguploadProgress").show(); 
     $(".imguploadProgressBar").width('0%'); 
     $(".imguploadMsg").html(""); 
     $(".imguploadProgressPercent").html("0%"); 
    }, 
    uploadProgress: function(event, position, total, percentComplete) 
    { 
     $(".imguploadProgressBar").width(percentComplete+'%'); 
     $(".imguploadProgressPercent").html(percentComplete+'%'); 

    }, 
    success: function() 
    { 
     $(".imguploadProgressBar").width('100%'); 
     $(".imguploadProgressPercent").html('100%'); 

    }, 
    complete: function(response) 
    { 
     alert('Complecion'); 
    }, 
    error: function() 
    { 
     $(".imguploadMsg").html("<font color='red'> ERROR: unable to upload files</font>"); 
    } 

    }; 

    $("#profpicform").ajaxForm(options); 

SEVER SIDE:

$output_dir = home_url()."/path/to/dir/"; 

if(isset($_FILES["profpic"])){ 
    if ($_FILES["profpic"]["error"] > 0){ 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
    }else{ 
     move_uploaded_file($_FILES["profpic"]["tmp_name"],$output_dir. $_FILES["profpic"]["name"]); 
     if(get_user_meta($_SESSION['userid'], 'user_profile_picture')==""){ 
      add_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']); 
     }else{ 
      update_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']); 
     } 
     echo "Uploaded File :".$_FILES["profpic"]["name"]; 
    } 
} 

他們在一個PHP文件中只發現。對於目錄文件夾的權限是777

回答

8

試試這個:

$destination_path = getcwd().DIRECTORY_SEPARATOR; 
$target_path = $destination_path . basename($_FILES["profpic"]["name"]); 
@move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path) 
+6

感謝夥計!幾乎沒有twerking,完美的工作! – Wadapmerth

+7

哈哈@「twerking」 – Rafael

+0

請你在深 –

相關問題