我正在尝试在我的Android应用程序中实现用户上传。由于某种原因,下面的代码似乎无法上传图像,即数据库条目已完成,但在文件夹中找不到图像。有人可以帮忙吗?我遵循了本教程:https://www.simplifiedcoding.net/android-upload-image-to-server/ 路径是正确的,因为我可以看到数据库条目中使用的路径。
<?php
require_once 'dbDetails.php';
$upload_path = 'usr_upload/';
$server_ip = gethostbyname(gethostname());
$upload_url = 'http://'.$server_ip.'/wallpaper/games/'.$upload_path;
$response = array();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['name']) and isset($_FILES['image']['name'])){
$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) or die('unable to connect to database ');
$name = $_POST['name'];
$fileinfo = pathinfo($_FILES['image']['name']);
$extension = $fileinfo['extension'];
$file_url = $upload_url . getFileName() . '.'.$extension;
$file_path = $upload_path. getFileName(). '.'.$extension;
try{
move_uploaded_file($_FILES['image']['tmp_name'], $file_path);
$sql = "INSERT INTO images (url, name) VALUES ('$file_url','$name')";
if(mysqli_query($con,$sql)){
$response['error'] = false;
$response['url'] = $file_url;
$response['name'] = $name;
}
}catch(Exception $e){
$response['error'] = false;
$response['message'] = $e->getMessage();
}
mysqli_close($con);
}else{
$response['error'] = true;
$response['message'] = 'Please choose a file';
}
echo json_encode($response);
}
function getFileName(){
$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) or die('Unable to connect');
$sql = "SELECT max(id) as id FROM images";
$result = mysqli_fetch_array(mysqli_query($con,$sql));
mysqli_close($con);
if($result['id']==null){
return 1;
}else{
return ++$result['id'];
}
}
创建的错误日志是:
PHP警告:move_uploaded_file():无法在第28行的DOMAINNAME.COM/wallpaper/games/usr_upload/upload.php中将'/ tmp / phpSv15lI'移动到'usr_upload / 3.jpg'
答案 0 :(得分:1)
我自己解决了,结果我在php文件中指定的链接是错误的。