简体   繁体   中英

Internal Server Error 500 upload image iOS php

I've read many people having the same problem, all were similar and had to do with my server settings, made sure the images dir has 755 permission, and for some reason im seeing my users subfolder set to 777, and i have created the php.ini file, i made sure my upload_max_filesize is set properly, and that my php.ini has proper settings. I've tried several different coding techniques from many different tutorials, all have outputted the same errors.

This is the error in my NSlog

myproject[11784:907] test <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator,
 webmaster@myproject.com and inform them of the time the error occurred,
and anything you might have done that may have
caused the error.</p>
<p>More information about this error may be available
in the server error log.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

Here is my objective-c code

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self  dismissViewControllerAnimated:YES completion:NULL];

NSData *dataImage = UIImageJPEGRepresentation(image.image, 0.8f);
NSString *urlString = @"http://www.myproject.com/myphpscript.php";
NSString *filename = [NSString stringWithFormat:@"%@.jpg", [MyClass str]];
NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.png\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"email"] dataUsingEncoding:NSASCIIStringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@",[MyClass str]] dataUsingEncoding:NSASCIIStringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString* returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"test %@",returnString);
}

and my PHP

<?php
// removed connection 
$mysqli = new mysqli($host, $username, $password, $database);
if ($stmt = $mysqli->prepare("INSERT INTO `user` SET imagepath=? WHERE email=?")) 
{
    $stmt->bind_param('ss', $albumname, $email);

$uploaddir = 'images/users/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

}
$albumname = $uploadfile;
    $email = $_POST['email'];
    $stmt->execute();
    $stmt->close(); 
    }


else {
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}

$mysqli->close();
?>

Error 500 is error on server side.

I would recommend to try upload from server to itself, that way you may get more info what's wrong. Also check error log from your web server, nginx, appache or what ever you have running, there you will find what's causing error.

While I can't see what the 500 error is from your code alone, I highly suggest you ensure your PHP ini file has error logging enabled and have a look through the log file. A 500 error in most cases is caused by a runtime error in the PHP engine which it will output to the log file, or if you have display_errors enabled, will output straight to the browser.

9 times out of 10 the error is easily fixable once you can read the log file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM