我在android中有以下代码
String uploadId = UUID.randomUUID().toString();
//Creating a multi part request
new MultipartUploadRequest(getContext(), uploadId, url_upload_cert)
.addFileToUpload(path, "pdf") //Adding file
.addParameter("name", name) //Adding text parameter to the request
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
和php
<?php
//importing dbDetails file
require_once __DIR__ . '/db_connect.php';
//this is our upload folder
$upload_path = 'android/';
//Getting the server ip
$server_ip = gethostbyname(gethostname());
//creating the upload url
//$upload_url = 'http://'.$server_ip.'/pavo_project/'.$upload_path;
$upload_url='/src';
//response array
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
//checking the required parameters from the request
if(isset($_POST['name']) and isset($_FILES['jpg']['name'])){
// connecting to db
$db = new DB_CONNECT();
//getting name from the request
$name = $_POST['name'];
//getting file info from the request
$fileinfo = pathinfo($_FILES['jpg']['name']);
//getting the file extension
$extension = $fileinfo['extension'];
//file url to store in the database
$file_url = $upload_url . $name . '.' . $extension;
//file path to upload in the server
$file_path = $upload_url . $name . '.'. $extension;
//trying to save the file in the directory
try{
//saving the file
move_uploaded_file($_FILES['jpg']['tmp_name'],$file_path);
}catch(Exception $e){
$response['error']=true;
$response['message']=$e->getMessage();
}
//closing the connection
// mysqli_close($db);
}else{
$response['error']=true;
$response['message']='Please choose a file';
}
//displaying the response
echo json_encode($response);
}
?>
当我运行android代码时,它成功运行,没有错误,但是当我检查src文件夹中的图像时,它不存在 当我使用邮递员测试脚本时,也会发生相同的情况 我在Windows上使用wamp服务器。 可能是什么原因造成的?
答案 0 :(得分:1)
检查文件夹的写入权限。
例如在Linux中,文件夹上方的某个级别,您可以使用以下命令:
sudo chmod -R 777 <your-folder-name>
这可能会使php代码将文件保存在其中,因为它将向每个用户授予写入/读取/删除的权限。
还可以检查 php.ini 文件,并检查以下行:
file_uploads = On
具有打开值
如果这不起作用,您可以检查默认的php.ini文件,然后尝试用该文件替换您的 php.ini 文件。