PHP to do the server, accept the client uploads the file, the server is how to receive

Source: Internet
Author: User
We use C + + in the local client, and then want to use PHP on the server side to write a program to accept the file storage, is not the server to write a listener, or how to link them to achieve data transmission


Reply to discussion (solution)

Add Multipart/form-data header with Post method
PHP will automatically recognize the uploaded files

If put, you need to read the file contents from the Php://input resource on the balance side

Add Multipart/form-data header with Post method
PHP will automatically recognize the uploaded files

If put, you need to read from the Php://input resources in the balance of the contents of the file can write a few lines of specific code, PHP I was just beginning to learn new, thank you

if (isset (the variable name specified at $_files[' upload ') {  move_uploaded_file (the variable name specified at the time of the upload ($_files[') ' [' Tmp_name '], ' target filename ')} else {  $s = file_get_contents (' php://input ');  if ($s) {    file_put_contents (' target filename ', $s);}  }

Add Multipart/form-data header with Post method
PHP will automatically recognize the uploaded files

If put, you need to read the contents of the file from the Php://input resource on the balance. I have a program here, and I don't know that.

if ($_server["Request_method"]== "POST")
{
Upload the largest file size.
$MAX _size = 20000000;
echo $MAX _size;
Sets 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 ');

Sets the allowed file types. Please add it in the format.
$FILE _exts = Array ('. zip ', '. jpg ', '. png ', '. gif ', '. Doc ', '. txt ', '. rar ', ' zip ');


/************************************************************
* Set Variable
************************************************************/
$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;
}

/************************************************************
* User request process, all the following upfile need to be consistent with VC client, can be replaced in bulk. See HotpimUploadDlg.cpp line No. 224.
************************************************************/


if ($_files[' upfile ')
{
$resource = fopen ("Log.txt", "a");
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, "."));

File size Check
if ($_files[' upfile ' [' Size '] > $MAX _size)
$message = "The file size is over 2MB.";
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;

File name Check
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 the standard file upload program, it is recommended to see the PHP manual to understand.

Suggested look at manual $_files there are examples

Well, thank you, I'm going to go check out the PHP manual.

  • Related Article

    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.