Swift 3 将图像上传到 PHP 失败
Posted
技术标签:
【中文标题】Swift 3 将图像上传到 PHP 失败【英文标题】:Swift 3 Upload image to PHP Fail 【发布时间】:2016-09-21 09:34:42 【问题描述】:上传图片失败 我通过 POST 将图像发送到 php
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
@IBOutlet var image: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
@IBAction func selectPicture(_ sender: AnyObject)
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(ImagePicker, animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)
@IBAction func upload_request(_ sender: AnyObject)
UploadRequest()
func UploadRequest()
let url = URL(string: "http://127.0.0.1/imgJSON/img.php")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (image.image == nil)
return
let image_data = UIImagePNGRepresentation(image.image!)
if(image_data == nil)
return
let body = NSMutableData()
let fname = "test.png"
let mimetype = "image/png"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let session = URLSession.shared
let task = URLSession.shared.dataTask(with: request as URLRequest) (
data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else
print("error")
return
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print(dataString)
task.resume()
func generateBoundaryString() -> String
return "Boundary-\(UUID().uuidString)"
在 PHP 中
if (move_uploaded_file($_FILES['file']['tmp_name'], "image.png"))
echo "File uploaded: ".$_FILES["file"]["name"];
【问题讨论】:
【参考方案1】:您的第二个屏幕截图显示了问题: 这是带有 PHP 警告的服务器回复,在您的情况下,apache/php 用户无权将上传的图像移动到目标目录,这将是根目录 (/) - 因为您没有指定目录/路径在哪里存储图像。请参阅 PHP 文档的Example 1。
所以你应该创建一个上传目录(例如 mkdir /Appli.../htdocs/uploads ,设置正确的权限(用于开发目的 chmod -R 777 上传 ) 并在您的 PHP 中指定目标目录:
if (move_uploaded_file($_FILES['file']['tmp_name'], "/Appli.../htdocs/uploads/image.png")) ...
【讨论】:
它在 php 中不起作用 if (move_uploaded_file($_FILES['file']['tmp_name'], "/Applications/XAMPP/xamppfiles/htdocs/image.png")) echo "File上传:".$_FILES["file"]["name"];我运行应用程序但显示dropbox.com/s/gd87ym4yfr0lao3/… 您现在将图像直接保存到您的 htdocs /Applications/XAMPP/xamppfiles/htdocs/ 请尝试以下操作: 对不起,我在这里打断了完整的评论:您现在将图像直接保存到您的 htdocs 文件夹:/Applications/XAMPP/xamppfiles/htdocs,请在您的 终端上尝试以下操作: mkdir /Applications/XAMPP/xamppfiles/htdocs/uploads && chmod -R 777 /Applications/XAMPP/xamppfiles/htdocs/uploads 然后在PHP上使用: if (move_uploaded_file($_FILES['文件']['tmp_name'], "/Applications/XAMPP/xamppfiles/htdocs/uploads/image.png")) 不工作,我测试 html 表单发送图像到 PHP dropbox.com/s/1br0v98znwlainr/3.png?dl=0 啊,好吧,该目录已经存在,所以只需执行以下操作: sudo chmod -R 777 /Applications/XAMPP/xamppfiles/htdocs/imgJSON 如果这是您要存储图像的目录。以上是关于Swift 3 将图像上传到 PHP 失败的主要内容,如果未能解决你的问题,请参考以下文章
PHP和Ajax将文件路径上传到Mysql并将重命名的图像保存到文件夹失败
使用 PHP 在 Swift 5 中使用 Alamofire 接收图像上传