2013-03-13 140 views
0

我有這樣的短,基本代碼:移動文件到目錄用PHP

<?php 
    $username = $_POST['User']; 
    $file = $_FILES['File']; 
    $size = $_FILES['File']['size']; 
    $name = $_FILES['File']['name']; 
    $temp_name = $file['tmp_name']; 
    $user_id = Data::getUserId($username); 
    $target_path = "/---/---/profile_pics/" . $name; 

    $sql = "INSERT INTO profile_photos (name, size, user_id) 
       VALUES ('{$name}', '{$size}', '{$user_id}');"; 
    mysqli_query($dbconnection, $sql); 

    move_uploaded_file($temp_name, $target_path); 
?> 

注意,這是測試代碼,所以我可能沒有遵循最佳實踐。

我得到正確的文件信息到我的MySQL數據庫。但是我沒有看到在目錄中移動的實際文件。

另外,數據庫IS會提取正確的文件大小和文件名,以便將某些內容傳遞給PHP,這是準確的。


我從Java傳遞文件。 這裏是爲此補充信息(萬一有人知道這一目的,但我不認爲這個問題是在這裏:

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url_select); 
File file = new File(imagePath); 
HttpResponse response = null; 
MultipartEntity entity = new MultipartEntity(); 
ContentBody cbFile = new FileBody(file, "image/jpeg"); 
StringBody sb; 

try { 
    sb = new StringBody(userName); 
    entity.addPart("User", sb); 
    entity.addPart("File", cbFile); 
    post.setEntity(entity); 
    response = client.execute(post);    
} 
+1

你確定正在生成正確的路徑嗎?你有寫權限到你的服務器? – 2013-03-13 19:33:08

+0

你說得對。我遇到了路徑上的常量變量問題。我使用godaddy,他們的絕對路徑最終是looong。 – KickingLettuce 2013-03-13 19:44:43

回答

1

PHP可能沒有權限寫入到目標目錄那倒。解釋爲什麼PHP有關於文件的信息,但無法將其移動到該位置

您可以嘗試在同一位置爲文件(fopen,fwrite等)編寫簡單的文件以驗證權限是否設置正確

如果您打開了PHP錯誤日誌記錄,則應該使用als o在日誌中看到關於這個的註釋。

祝你好運。

0

它是仔細檢查你的路徑小子的路徑。這是我的問題的答案。代碼是正確的。