php做服务器,接受客户端上传的文件,服务器是怎么接收的
我们用C++在本地写的客户端,然后想用PHP在服务器端写个程序把这个文件接受存储,是不是在服务器要写个监听程序,还是要怎么来把他们链接起来实现数据传输
回复讨论(解决方案)
用 post 方式加 multipart/form-data 头
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容
用 post 方式加 multipart/form-data 头
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容 能不能写几行具体的代码,PHP我也是刚开始学新手,谢谢了
if(isset($_FILES['上传时指定的变量名']['tmp_name')) { move_uploaded_file($_FILES['上传时指定的变量名']['tmp_name'], '目标文件名');}else { $s = file_get_contents('php://input'); if($s) { file_put_contents('目标文件名', $s); }}
用 post 方式加 multipart/form-data 头
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容 我这里有个程序,我不懂人家这个
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//上传最大文件的大小。
$MAX_SIZE = 20000000;
echo $MAX_SIZE;
//设置允许的 Mime 类型.
$FILE_MIMES = array('image/jpeg','image/jpg','image/gif'
,'image/png','application/msword','text/plain','application/octet-stream','application/x-zip-compressed');
//设置允许的文件类型。请按照格式添加。
$FILE_EXTS = array('.zip','.jpg','.png','.gif','.doc','.txt','.rar','zip');
/************************************************************
* 设置变量
************************************************************/
$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 ="";
/************************************************************
* 创建上传目录
************************************************************/
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;
}
/************************************************************
* 用户请求进程,下面所有用到的 upfile 需要与VC客户端一致,可以批量替换。见HotpimUploadDlg.cpp第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,"."));
//文件大小检查
if ( $_FILES['upfile']['size'] > $MAX_SIZE)
$message = "The file size is over 2MB.";
//文件类型/扩展名检查
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;
//文件名字检查
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;
}
?>
这个是标准的文件上传程序,建议去看看php手册就明白了。
建议看看手册 $_FILES 有说明有例子
嗯谢谢各位了,我继续去看看PHP手册

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
