我知道很多人一直在这里问类似的问题,但不知怎的,我仍然没有让它发挥作用。 为了简单起见,我只是试图将之前加载的UIImage存储在objectiveC(使用C4框架)中,如下所示:
-(void)setup {
UIImage *myImage=[UIImage imageNamed:@"image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
// setting up the URL to post to
NSString *urlString = @"my full server address/imageupload.php";
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 *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"dr.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
C4Log(@"returnString: %@", returnString);
}
然后我的upload.php看起来像这样:
<?php
$uploaddir = './'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n ";
} else {
echo "Temp file not uploaded. \r\n";
}
move_uploaded_file($_FILES['userfile']['tmp_name'], $newName);
$postsize = ini_get('post_max_size');
$canupload = ini_get('file_uploads');
$tempdir = ini_get('upload_tmp_dir');
$maxsize = ini_get('upload_max_filesize');
echo "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" . $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;
?>
由于某种原因看起来'upload_tmp_dir'是空的,但我不知道如何解决它... 这是回声:
Temp file uploaded.
MY SERVER/dr.jpg
size:31476
type:application/octet-stream
postsize:100M
canupload:1
tempdir:
maxsize:100M
任何tipps?
答案 0 :(得分:0)
使用脚本中的完整路径到上传目录,也只在上传临时文件时移动临时文件。例如:
<?php
$uploaddir = '/Volumes/Web/mlab/mlabwww/UrbanAlphabets/'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999);
$newName = $uploadDir . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n ";
move_uploaded_file($_FILES['userfile']['tmp_name'], $newName);
$postsize = ini_get('post_max_size');
$canupload = ini_get('file_uploads');
$tempdir = ini_get('upload_tmp_dir');
$maxsize = ini_get('upload_max_filesize');
echo "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" . $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;
} else {
echo "Temp file not uploaded. \r\n";
}
?>
答案 1 :(得分:0)
临时目录将始终为空。 所以你必须在脚本中移动文件..
并且不要查找文件临时目录,因为它不会在那里寻找你移动的地方..