我有這樣的短,基本代碼:移動文件到目錄用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);
}
你確定正在生成正確的路徑嗎?你有寫權限到你的服務器? – 2013-03-13 19:33:08
你說得對。我遇到了路徑上的常量變量問題。我使用godaddy,他們的絕對路徑最終是looong。 – KickingLettuce 2013-03-13 19:44:43