Php is used as a server to accept files uploaded by the client. how does the server receive files?

Source: Internet
Author: User
Php acts as a server and accepts files uploaded by the client. how does the server receive files written locally by C ++, then I want to use PHP to write a program on the server to store the file. Do I need to write a listener on the server? how can I link them for data transmission?


Reply to discussion (solution)

Add the multipart/form-data header in post mode
Php automatically identifies uploaded files

If you use the put method, you need to read the file content from the php: // input resource on the balanced object.

Add the multipart/form-data header in post mode
Php automatically identifies uploaded files

If you use the put method, you need to read the file content from the php: // input resource on the balanced item side. can you write a few lines of specific code? PHP is also a beginner at the beginning. thank you.

If (isset ($ _ FILES ['variable name specified during Upload '] ['tmp _ name ')) {move_uploaded_file ($ _ FILES ['variable name specified during Upload '] ['tmp _ name'], 'destination filename ');} else {$ s = file_get_contents ('php: // input'); if ($ s) {file_put_contents ('Destination filename ', $ s );}}

Add the multipart/form-data header in post mode
Php automatically identifies uploaded files

If you use the put method, you need to read the file content from the php: // input resource on the balanced object. I have a program here.

If ($ _ SERVER ["REQUEST_METHOD"] = "POST ")
{
// Upload the maximum file size.
$ MAX_SIZE = 20000000;
Echo $ MAX_SIZE;
// Set the allowed Mime type.
$ FILE_MIMES = array ('image/jpeg ', 'image/jpg', 'image/GIF'
, 'Image/png ', 'application/msword', 'text/plain', 'application/octet-stream', 'application/x-zip-compressed ');

// Set the allowed file types. Please add according to the format.
$ FILE_EXTS = comment ', 'Zip ');


/*************************************** *********************
* Set variables
**************************************** ********************/
$ Site_name = $ _ SERVER ['http _ host'];
$ Url_dir = "http: //". $ _ SERVER ['http _ host']. dirname ($ _ SERVER ['php _ SELF ']);
$ Url_this = "http: //". $ _ SERVER ['http _ host']. $ _ SERVER ['php _ SELF '];

$ Upload_dir = "files /";
$ Upload_url = $ url_dir. "/files /";
$ Message = "";

/*************************************** *********************
* Create an upload Directory
**************************************** ********************/
If (! Is_dir ("files ")){
If (! Mkdir ($ upload_dir ))
Die ("upload_files directory doesn't exist and creation failed ");
If (! Chmod ($ upload_dir, 0755 ))
Die ("change permission to 755 failed .");
Echo $ upload_dir;
}

/*************************************** *********************
* The User request process. all the following upfiles must be the same as those of the VC client. they can be replaced in batches. See row 224th of HotpimUploadDlg. cpp.
**************************************** ********************/


If ($ _ FILES ['upfile'])
{
$ Resource = fopen ("log.txt", "");
Fwrite ($ resource, date ("Ymd h: I: s"). "UPLOAD-$ _ SERVER [REMOTE_ADDR]"
. $ _ FILES ['upfile'] ['name']. ""
. $ _ FILES ['upfile'] ['type']. "\ n ");
Fclose ($ resource );

$ File_type = $ _ FILES ['upfile'] ['type'];
$ File_name = $ _ FILES ['upfile'] ['name'];
$ File_ext = substr ($ file_name, strrpos ($ file_name ,"."));

// Check the file size
If ($ _ FILES ['upfile'] ['size']> $ MAX_SIZE)
$ Message = "The file size is over 2 MB .";
// File type/extension check
Else if (! In_array ($ file_type, $ FILE_MIMES )&&! In_array ($ file_ext, $ FILE_EXTS ))
$ Message = "Sorry, $ file_name ($ file_type) is not allowed to be uploaded .";
Else
$ Message = do_upload ($ upload_dir, $ upload_url );
}

Else if (! $ _ FILES ['upfile']);
Else
$ Message = "Invalid File Specified .";

}
Function do_upload ($ upload_dir, $ upload_url)
{
$ Temp_name = $ _ FILES ['upfile'] ['tmp _ name'];
$ File_name = $ _ FILES ['upfile'] ['name'];
$ File_name = str_replace ("\", "", $ file_name );
$ File_name = str_replace ("'", "", $ file_name );
$ File_path = $ upload_dir. $ file_name;

// Check the file name
If ($ file_name = ""){
$ Message = "Invalid File Name Specified ";
Return $ message;
}

$ Result = move_uploaded_file ($ temp_name, $ file_path );
If (! Chmod ($ file_path, 0755 ))
$ Message = "change permission to 755 failed .";
Else
$ Message = ($ result )? "$ File_name uploaded successfully .":
"Somthing is wrong with uploading a file .";
Return $ message;
}
?>

This is a standard file upload program. it is recommended that you check the php Manual to understand it.

We recommend that you check the manual $ _ FILES with examples.

Thank you. I will continue to read the PHP Manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.