如何使用react native expo image将图像上载到本地服务器PHP/MySQL数据库

如何使用react native expo image将图像上载到本地服务器PHP/MySQL数据库,php,reactjs,database,react-native,expo,Php,Reactjs,Database,React Native,Expo,我正在使用expo客户端制作一个移动应用程序,允许用户上传图像或从相机拍摄,然后图像保存在PHP/数据库MySQL上的本地服务器上。如果我使用世博会,我该怎么做 例如react native中的代码(保存到PHP本地服务器,但不保存数据库) import React,{Component}来自'React'; 进口{ 活动指示器, 按钮 剪贴板, 形象,, 分享, 状态栏, 样式表, 文本, 可触摸不透明度, 看法 }从“反应本机”; 从“expo”导入{Constants}; 从“世博会图像采

我正在使用expo客户端制作一个移动应用程序,允许用户上传图像或从相机拍摄,然后图像保存在PHP/数据库MySQL上的本地服务器上。如果我使用世博会,我该怎么做

例如react native中的代码(保存到PHP本地服务器,但不保存数据库)

import React,{Component}来自'React';
进口{
活动指示器,
按钮
剪贴板,
形象,,
分享,
状态栏,
样式表,
文本,
可触摸不透明度,
看法
}从“反应本机”;
从“expo”导入{Constants};
从“世博会图像采集器”导入*作为图像采集器;
从“expo Permissions”导入*作为权限;
导出默认类应用程序扩展组件{
状态={
图像:空,
上传:错,
};
render(){
让{
形象
}=本州;
返回(
示例:上载ImagePicker结果
{this.\u maybeRenderImage()}
{this.\u可能欠加载overlay()}
);
}
_可能欠加载Overlay=()=>{
if(this.state.uploading){
返回(
);
}
};
_maybeRenderImage=()=>{
让{
形象
}=本州;
如果(!图像){
返回;
}
返回(
{image}
);
};
_份额=()=>{
分享({
消息:this.state.image,
标题:“查看这张照片”,
url:this.state.image,
});
};
_copyToClipboard=()=>{
Clipboard.setString(this.state.image);
警报(“已将图像URL复制到剪贴板”);
};
_takePhoto=async()=>{
常数{
状态:cameraPerm
}=wait Permissions.askAsync(Permissions.CAMERA);
常数{
状态:cameraRollPerm
}=等待权限.askAsync(权限.CAMERA\u ROLL);
//仅当用户允许使用摄影机和摄影机卷时
if(cameraPerm===‘已授予’&&cameraRollPerm===‘已授予’){
让pickerResult=等待ImagePicker.launchCameraAsync({
允许编辑:对,
相位:[4,3],
});
此._handleImagePicked(pickerResult);
}
};
_pickImage=async()=>{
常数{
状态:cameraRollPerm
}=等待权限.askAsync(权限.CAMERA\u ROLL);
//仅当用户允许滚动摄影机时
如果(cameraRollPerm===‘已授予’){
让pickerResult=等待ImagePicker.launchImageLibraryAsync({
允许编辑:对,
相位:[4,3],
});
此._handleImagePicked(pickerResult);
}
};
_handleImagePicked=async pickerResult=>{
让uploadResponse,uploadResult;
试一试{
这是我的国家({
上传:正确
});
如果(!pickerResult.cancelled){
uploadResponse=等待uploadImageAsync(pickerResult.uri);
uploadResult=等待uploadResponse.json();
这是我的国家({
图像:uploadResult.location
});
}
}捕获(e){
log({uploadResponse});
log({uploadResult});
console.log({e});
警报('上载失败,抱歉:(');
}最后{
这是我的国家({
上传:假
});
}
};
}
异步函数uploadImageAsync(uri){
让我们来看看http://192.168.0.18/upload-api/uploading.php';
让uriParts=uri.split('.');
让fileType=uriParts[uriParts.length-1];
设formData=new formData();
append('fileToUpload'{
乌里,
名称:`fileToUpload.${fileType}`,
类型:`image/${fileType}`,
});
让选项={
方法:“POST”,
正文:formData,
标题:{
接受:'application/json',
“内容类型”:“多部分/表单数据”,
},
};
返回获取(apirl、选项);
}
这是我的PHP

  <?php
   $target_dir = 'uploads/';
   $target_file = $target_dir . basename($_FILES['fileToUpload']['name']);

   $status = array();

   if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) {
   $status['status']=1;
   $status['description']='upload success';
  } else {
   $status['status']=0;
   $status['description']='upload failed';
  }
   echo json_encode($status);

  ?> 

有什么解决方法吗?谢谢你可以用来上传图片

var photo = {
  uri: selectImg.localUri,
  type: 'image/jpeg',
  name: 'photo.jpg',
};

var form = new FormData();
form.append("ProfilePicture", photo);

fetch(
  Constants.API_USER + 'me/profilePicture',
  {
    body: form,
    method: "PUT",
    headers: {
      'Content-Type': 'multipart/form-data',
      'Authorization': 'Bearer ' + user.token
    }
  }
).then((response) => response.json())
.catch((error) => {
  alert("ERROR " + error)
})
.then((responseData) => {
  alert("Succes "+ responseData)
}).done();

您可以使用将图像上载到您的数据库。好的。如果我使用的是Thread?它仍然可以上载到我的服务器吗?Thread只是一个包管理器。您必须设置后端才能将其上载到您的数据库。谢谢您的回答。它是否正在使用method post?
var photo = {
  uri: selectImg.localUri,
  type: 'image/jpeg',
  name: 'photo.jpg',
};

var form = new FormData();
form.append("ProfilePicture", photo);

fetch(
  Constants.API_USER + 'me/profilePicture',
  {
    body: form,
    method: "PUT",
    headers: {
      'Content-Type': 'multipart/form-data',
      'Authorization': 'Bearer ' + user.token
    }
  }
).then((response) => response.json())
.catch((error) => {
  alert("ERROR " + error)
})
.then((responseData) => {
  alert("Succes "+ responseData)
}).done();