I recently encountered the image upload function in my project. I have been working on it for two days and will share it with you here.
First, you need to upload an image instead of directly uploading the image through a URL. Instead, you need to convert the image into a binary file.
There are two methods in IOS that can directly convert images into binary
UIImage * image = [UIImage imageNamed: @ "8fca9.jpg"];
NSData * data = UIImagePNGRepresentation (image );
NSData * data2 = UIImageJPEGRepresentation (image, 1 );
Is to use one of the two methods to convert the image into binary data
Then, upload data to the network. Here I use a third-party library ASIFormDataRequest for network requests
The Code is as follows:
// Initialize the request with a URL
ASIFormDataRequest * request = [[ASIFormDataRequest alloc] initWithURL: url];
// Set proxy
[Request setDelegate: self];
// Add data for the uploaded object
[Request addData: data withFileName: @ "testimage.png" andContentType: @ "image/png" forKey: @ "file"];
// Name saved after upload // storage type // form name and corresponding php File
[Request startAsynchronous]; // start. Asynchronous
This is done, isn't it easy. In fact, this is only half done.
The following describes how the server processes the request.
I wrote a PHP file to process this network request.
Header ("Content-type: text/html; charset = UTF-8 ");
/* Bool print_r (mixed expression [, bool return ])
Note: The return parameter is added in PHP 4.3.0.
Print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.
Remember, print_r () will move the array pointer to the last edge.
*/
/* $ _ Detailed description of files
The master wants to upload an abc.mp3 file to the local directory where the binary file is uploaded. The server needs to obtain the relevant information of the file, and the file is obtained through the variable $ _ files.
$ _ FILES ['userfile'] ['name']
The original name of the client machine file.
$ _ FILES ['userfile'] ['type']
The MIME type of the file, which must be supported by the browser, for example, "image/gif ".
$ _ FILES ['userfile'] ['SIZE']
Size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name']
Temporary File Name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error']
The Error Code related to the file upload. ['Error'] is added in PHP 4.2.0.
Note: Before PHP 4.1.0, the array name is $ HTTP_POST_FILES, which is not an automatic global variable like $ _ FILES. PHP 3 does not support the $ HTTP_POST_FILES array.
If no uploaded file is selected in the form, the value of the PHP variable $ _ FILES ['userfile'] ['SIZE'] is 0, $ _ FILES ['userfile'] ['tmp _ name'] will be none.
*/
Print_r ($ _ FILES ['file']);
$ Filename = $ _ FILES ['file'] ['name'];
If (! $ _ FILES ['file'] ['error'])
{
/* Move_uploaded_file
Http://www.w3school.com.cn/php/func_filesystem_move_uploaded_file.asp
*/
// Write the object to the specified path
If (move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], "./image/user/". $ filename )){
Echo "File Uploaded ";
}
} Else {
Echo "File Upload error ";
}
?>
The above is the complete network request and PHP file.
Note that you must obtain
Read and Write PermissionsAnd is not just a folder permission,
Is the permission for all folders in the entire path.
Today, I will share with you a small knowledge-LC.