gpt4 book ai didi

php - 无法将照片上传到我的应用程序到网络服务器

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

我创建了viewcontroller以从图像选择器上传图片并将其发送到php webserver,但没有上传到服务器。
这是我的代码

uploadViewcontroller.m

NSData *picdata = UIImageJPEGRepresentation(imgShow.image, 90);
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:@"http://www.mytestwebsite.com/upload2.php"]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: from-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:picdata]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

和我的PHP代码
<?php
$uploaddir = "post_comment/original/";
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $randomNumber . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 3000000) {
exit("Your file is too large.");
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "upload finished" ;
}
?>

最佳答案

检查是否有效...对我有用

$image = $_POST['userfile'];
$image = str_replace("<","",$image);
$image = str_replace(">","",$image);
$image = str_replace(" ","",$image);
$image = pack("H*", $image);
$filename = "image.png";
$f = fopen($filename,'wb');
fwrite($f, $image);
fclose($f);

关于php - 无法将照片上传到我的应用程序到网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472436/

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