M试图创建一个上传和下载脚本,用户上传文件并将其插入到数据库中。我知道这不是一个好方法,但它是唯一适合我的方法。
在上传脚本中,它上传了.txt,.doc等文件,但它不会上传zip文件和图像文件。请帮助我。
这是上传脚本的代码
- (void)beginSendImage:(UIImage *)image
{
//把图片转换成imageDate格式
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
//传送路径
NSString *urlString = ShareManager.systemUrl;
if([urlString rangeOfString:@"http"].location == NSNotFound){
urlString = [NSString stringWithFormat:@"http://%@/JBIZ/AppUploadAction",urlString];
}else{
urlString = [NSString stringWithFormat:@"%@/JBIZ/AppUploadAction",urlString];
}
//建立请求对象
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=\vim_go.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]];
[request setHTTPBody:body];
//上传文件开始
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//获得返回值
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
这是下载脚本
<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'myTable');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `file` (
`name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>
答案 0 :(得分:0)
您的代码不限制上传(它确实应该),因此PHP配置可能是负责任的。 php.ini配置文件不限制上传的文件类型,因此罪魁祸首可能是图像和zip文件的文件大小。检查php.ini文件(或使用phpinfo()
),看看最大上传大小是多少。
答案 1 :(得分:0)
在.htaccess中添加此项并检查
AddType application/zip .zip
AddType image/gif .gif .GIF
AddType image/ief .ief
AddType image/jpeg .jpeg .jpg .jpe .JPG
AddType image/tiff .tiff .tif
它将启用图像和zip mime类型。