To upload the image file to a web form

后端 未结 3 470
一个人的身影
一个人的身影 2020-12-18 13:50

I do have some image files(.png files) in document directory(iPhone). I am viewing web forms(.aspx) on the UIWebView. when I click on the submit button of the form, i just w

相关标签:
3条回答
  • 2020-12-18 14:05

    if you are trying to upload file from iphone safari. you are screwed, there is no support to upload file from iPhone safari.

    <input type="FILE"/> will be disable.

    0 讨论(0)
  • 2020-12-18 14:22

    Solved my own problem! Last echo line is what needed to be adjusted!

    <?php
    $uploaddir = './';      //Uploading to same directory as PHP file  ./
    $file = basename($_FILES['userfile']['name']);
    $uploadFile = $file;
    $randomNumber = rand(0, 99999); 
    $newName = $uploadDir . $randomNumber . $uploadFile;
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    
    } else {
        echo "Temp file not uploaded. \r\n";
    }
    
    if ($_FILES['userfile']['size']> 3000000000000000) {
    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 "http://localhost:8888/wouldYa/images/{$randomNumber}{$file}" . "\r\n";
    }
    ?>
    

    Now I am in the mist of saving the Str in Xcode, to another PHP file to insert the filename to that user's section in my table, specifically the display picture section, this should be no hard and implemented :-)

    If you are interested in my full source code, please let me know, I know it is not that secure yet but soon it will.

    0 讨论(0)
  • 2020-12-18 14:24

    You will need to modify these examples to work for you, but they are working examples.

    Objective-C

    MyViewController.m:

    - (void) upload
    {
        NSString *urlString = @"http://www.yourwebsite.com/uploads/upload.php";
        NSData *data = /* turn your png into NSData */
    
        // setting up the request object now
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
        /* now lets create the body of the post */
        NSString *content = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.png\"\r\n",@"yourPng"];
        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
        [body appendData:[[NSString stringWithString:content] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:data]];
        [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];  
    }
    

    PHP

    upload.php:

    <?php
    //assuming upload.php and upload folder are in the same dir
    $uploaddir = 'uploads/';
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    $product = $_GET[product];
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name']))
    {
        echo "PNG uploaded. \r\n";
    } else {
        echo "PNG not uploaded. \r\n";
    }
    
    if ($_FILES['userfile']['size']> 300000)     //Limiting image at 300K
    {
        exit("Your file is too large."); 
    }
    
    // Add support here for PNG files:
    if ((!($_FILES['userfile']['type'] == "text/plain")) &&  //Also allowing 
        (!($_FILES['userfile']['type'] == "text/plist")) &&  //plist files
        (!($_FILES['userfile']['type'] == "text/html")))     //HTML files
    {
        exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you uploaded."); 
    }
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        $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 "http://www.yourwebsite.com/uploads/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题