我有这样的短,基本代码:移动文件到目录用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