gpt4 book ai didi

java - 使用 PHP 将文件移动到目录

转载 作者:行者123 更新时间:2023-12-01 14:46:33 26 4
gpt4 key购买 nike

我有这个简短的基本代码:

<?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 数据库中。但我没有将实际文件移动到目录中。

此外,数据库正在提取正确的文件大小和文件名,因此传递给 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);
}

最佳答案

PHP 可能没有写入目标目录的权限。这可以解释为什么 PHP 拥有有关该文件的信息,但无法将其移动到该位置。

您可以尝试在同一位置向文件(fopen、fwrite 等)写入一些简单的内容,以验证权限设置是否正确。

如果您打开了 PHP 错误日志记录,您还应该在日志中看到有关此内容的注释。

祝你好运。

关于java - 使用 PHP 将文件移动到目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15394453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com