7 个解决方案
#1
用 post 方式加 multipart/form-data 头
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容
#2
能不能写几行具体的代码,PHP我也是刚开始学新手,谢谢了
#3
if(isset($_FILES['上传时指定的变量名']['tmp_name')) {
move_uploaded_file($_FILES['上传时指定的变量名']['tmp_name'], '目标文件名');
}else {
$s = file_get_contents('php://input');
if($s) {
file_put_contents('目标文件名', $s);
}
}
#4
我这里有个程序,我不懂人家这个
<?php
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
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;
}
?>
#5
这个是标准的文件上传程序,建议去看看php手册就明白了。
#6
建议看看手册 $_FILES 有说明有例子
#7
嗯谢谢各位了,我继续去看看PHP手册
#1
用 post 方式加 multipart/form-data 头
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容
php 会自动识别上传的文件
如果用 put 方式,则需在 平衡品端从 php://input 资源读取文件内容
#2
能不能写几行具体的代码,PHP我也是刚开始学新手,谢谢了
#3
if(isset($_FILES['上传时指定的变量名']['tmp_name')) {
move_uploaded_file($_FILES['上传时指定的变量名']['tmp_name'], '目标文件名');
}else {
$s = file_get_contents('php://input');
if($s) {
file_put_contents('目标文件名', $s);
}
}
#4
我这里有个程序,我不懂人家这个
<?php
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
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;
}
?>
#5
这个是标准的文件上传程序,建议去看看php手册就明白了。
#6
建议看看手册 $_FILES 有说明有例子
#7
嗯谢谢各位了,我继续去看看PHP手册