首页 > 编程知识 正文

关于php跨域shang传文件的信息

时间:2023-12-05 14:04:52 阅读:312258 作者:MTWB

本文目录一览:

  • 1、php怎么通服务器跨域上传图片
  • 2、PHP跨域上传的几种方法
  • 3、用php如何把一些文件和图片上传到另一指定的服务器
  • 4、用php如何跨越网络传输文件?
  • 5、用webuploader怎么解决跨域上传文件的问题

php怎么通服务器跨域上传图片

5.2下 把你的存放路径打出来看看就知道了,看路径是否正确,这种情况下很容易出错

PHP跨域上传的几种方法

方法一:

文件夹:/home/web/attachments

虚拟二级目录到/home/web/zxsv/下(支持同局域网的服务器)

这样多个子域名进行上传的设计时,只需要attachments目录映射为相关的域名的二级目录,这样就可实现多个子域名共享一个附件服务器了,这种方法最好是用局域网中的附件服务器,这样流量是分开的,当然访问附件的域名是apache,ngixn,IIS等的虚拟二级目录就不说了,好处是现有程序不做任何修改,唯一坏处就是两台服务器必须在一个局域网中,当然你用单台也就没这个问题了

方法二:FTP同步更新

PHP是支持FTP的,给个FTP类里面(不是我写的,只是加了个建立多级目录),自己看着办吧,上传后调用FTP类,同步到FTP服务器中,好处是现有程序只需要在上传那段加上FTP上传就行了,坏处就是一定要支持FTP

?php

$ftp=new Ftp;

//print_r($ftp-nlist(”"));

$ftp-makedir(”3″);

//$ftp-put(”comment.php”,”1.txt”);

$ftp-bye();

//R FTP 处理;

class ftp {

var $ftpUrl = ‘’;

var $ftpUser = ‘zxsv’;

var $ftpPass = ‘111111′;

var $ftpDir = ‘/zxsv/’;

var $ftpR = ”; //R ftp资源;

var $status = ”;

//R 1:成功;2:无法连接ftp;3:用户错误;

function ftp() {

if ($this-ftpR = ftp_connect($this-ftpUrl, 21)) {

if (ftp_login($this-ftpR, $this-ftpUser, $this-ftpPass)) {

if (!empty($this-ftpDir)) {

ftp_chdir($this-ftpR, $this-ftpDir);

}

ftp_pasv($this-ftpR, true);//R 启用被动模式;

$status = 1;

} else {

$status = 3;

}

} else {

$status = 2;

}

}

//R 切换目录;

function cd($dir) {

return ftp_chdir($this-ftpR, $dir);

}

//建立目录

function mkdir($dir){

return ftp_mkdir($this-ftpR, $dir);

}

function makedir($dir) {

if(!$dir) return 0;

$dir = str_replace( “\”, “/”, $dir );

$mdir = “”;

foreach(explode( “/”, $dir ) as $val ) {

$mdir .= $val.”/”;

if( $val == “..” || $val == “.” ) continue;

if(!@mkdir($mdir)){

echo “创建目录 [".$mdir."]失败.”;

//exit;

}

}

return true;

}

//删除目录

function rmdir($dir){

return ftp_rmdir($this-ftpR, $dir);

}

//R 返回当前路劲;

function pwd() {

return ftp_pwd($this-ftpR);

}

//R 上传文件;

function put($localFile, $remoteFile = ”) {

if ($remoteFile == ”) {

$remoteFile = end(explode(’/', $localFile));

}

$res = ftp_nb_put($this-ftpR, $remoteFile, $localFile, FTP_BINARY);

print_r($res);

while ($res == FTP_MOREDATA) {

$res = ftp_nb_continue($this-ftpR);

}

if ($res == FTP_FINISHED) {

return true;

} elseif ($res == FTP_FAILED) {

return false;

}

}

//R 下载文件;

function get($remoteFile, $localFile = ”) {

if ($localFile == ”) {

$localFile = end(explode(’/', $remoteFile));

}

if (ftp_get($this-ftpR, $localFile, $remoteFile, FTP_BINARY)) {

$flag = true;

} else {

$flag = false;

}

return $flag;

}

//R 文件大小;

function size($file) {

return ftp_size($this-ftpR, $file);

}

//R 文件是否存在;

function isFile($file) {

if ($this-size($file) = 0) {

return true;

} else {

return false;

}

}

//R 文件时间

function fileTime($file) {

return ftp_mdtm($this-ftpR, $file);

}

//R 删除文件;

function unlink($file) {

return ftp_delete($this-ftpR, $file);

}

function nlist($dir = ‘/service/resource/’) {

return ftp_nlist($this-ftpR, $dir);

}

//R 关闭连接;

function bye() {

return ftp_close($this-ftpR);

}

}

?

用php如何把一些文件和图片上传到另一指定的服务器

一个实例:

首先,在自己台式机和笔记本上都开通了ftp,这个不会的同学可以网上查serv-u,相关教程肯定不少的。

然后在台式机本地做了个测试:

$ftp_server = "192.168.1.100";

$ftp_user_name = "laohu";

$ftp_user_pass = "123456";

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

$file = 'test.txt';

$remote_file = '/test/a.txt';

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {

echo "文件移动成功n";

} else {

echo "移动失败n";

}

ftp_close($conn_id);

运行后:文件移动成功。

要的就是这个效果了,之后用台式机做程序服务器,上传附件时全用ftp方法上传至笔记本上,笔记本ip是105,相应代码如下:

if (is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {

$ftp_server = "192.168.1.105";

$ftp_user_name = "lesley";

$ftp_user_pass = "123456";

$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

$file = $_FILES['uploadfile']['tmp_name'];

$remote_file = '/test/'.$_FILES['uploadfile']['name'];

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {

echo "文件:".$_FILES['uploadfile']['name']."上传成功n";

} else {

echo "上传失败n";

}

ftp_close($conn_id);

}

对应的前台页面代码:

form action="uploadfile.php" method="post" enctype="multipart/form-data"

input type="file" name="uploadfile" id="uploadfile" /

input type="submit" name="submit" value="submit" /

/form

运行后确实成功。

需要注意:

在用ftp_put方法时,第四个参数传送模式,需要用FTP_BINARY(二进制模式),用FTP_ASCII(文本模式)时,图片能上传但无法显示,其他文件重命名、中文乱码解决、上传权限控制等,就不在此提及了。

用php如何跨越网络传输文件?

直接传?你是指你把另一台服务器的路径当成本地路径直接写入吗??我这里提供两种方法。。

1。另一台服务器开 FTP 服务,主服务器通过 PHP 与 FTP 服务器对接,实现文件传输。

2。另一台服务器上放置一个 PHP 程序。用来接收数据。主服务器通过 PHP 以 POST 方式把文件提交到另一台服务器。

用webuploader怎么解决跨域上传文件的问题

最近研究了下大文件上传的方法,找到了webuploader js 插件进行大文件上传,大家也可以参考这篇文章进行学习:《Web Uploader文件上传插件使用详解》使用使用webuploader分成简单直选要引入!--引入CSS-- link rel="stylesheet" type="text/css" href="webuploader文件夹/webuploader.css" !--引入JS-- script type="text/javascript" src="webuploader文件夹/webuploader.js"/script HTML部分div id="uploader" class="wu-example" !--用来存放文件信息-- div id="thelist" class="uploader-list"/div div class="btns" div id="picker"选择文件/div button id="ctlBtn" class="btn btn-default"开始上传 /button /div /div 初始化Web UploaderjQuery(function() { $list = $('#thelist'), $btn = $('#ctlBtn'), state = 'pending', uploader; uploader = WebUploader.create({ // 不压缩image resize: false, // swf文件路径 swf: 'uploader.swf', // 文件接收服务端。 server: upload.php, // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: '#picker', chunked: true, chunkSize:2*1024*1024, auto: true, accept: { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', mimeTypes: 'image/*' } }); upload.php处理以下是根据别人的例子自己拿来改的php 后台代码header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; // finish preflight CORS requests here } if ( !empty($_REQUEST[ 'debug' ]) ) { $random = rand(0, intval($_REQUEST[ 'debug' ]) ); if ( $random === 0 ) { header("HTTP/1.0 500 Internal Server Error"); exit; } } // header("HTTP/1.0 500 Internal Server Error"); // exit; // 5 minutes execution time @set_time_limit(5 * 60); // Uncomment this one to fake upload time // usleep(5000); // Settings // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; $targetDir = 'uploads'.DIRECTORY_SEPARATOR.'file_material_tmp'; $uploadDir = 'uploads'.DIRECTORY_SEPARATOR.'file_material'; $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // Create target dir if (!file_exists($targetDir)) { @mkdir($targetDir); } // Create target dir if (!file_exists($uploadDir)) { @mkdir($uploadDir); } // Get a file name if (isset($_REQUEST["name"])) { $fileName = $_REQUEST["name"]; } elseif (!empty($_FILES)) { $fileName = $_FILES["file"]["name"]; } else { $fileName = uniqid("file_"); } $oldName = $fileName; $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; // $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName; // Chunking might be enabled $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1; // Remove old temp files if ($cleanupTargetDir) { if (!is_dir($targetDir) !$dir = opendir($targetDir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); } while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; // If temp file is current file proceed to the next if ($tmpfilePath == "{$filePath}_{$chunk}.part" $tmpfilePath == "{$filePath}_{$chunk}.parttmp") { continue; } // Remove temp file if it is older than the max age and is not the current file if (preg_match('/.(partparttmp)$/', $file) (@filemtime($tmpfilePath) time() - $maxFileAge)) { @unlink($tmpfilePath); } } closedir($dir); } // Open temp file if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } if (!empty($_FILES)) { if ($_FILES["file"]["error"] !is_uploaded_file($_FILES["file"]["tmp_name"])) { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); } // Read binary input stream and append it to temp file if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } else { if (!$in = @fopen("php://input", "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($out); @fclose($in); rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part"); $index = 0; $done = true; for( $index = 0; $index $chunks; $index++ ) { if ( !file_exists("{$filePath}_{$index}.part") ) { $done = false; break; } } if ( $done ) { $pathInfo = pathinfo($fileName); $hashStr = substr(md5($pathInfo['basename']),8,16); $hashName = time() . $hashStr . '.' .$pathInfo['extension']; $uploadPath = $uploadDir . DIRECTORY_SEPARATOR .$hashName; if (!$out = @fopen($uploadPath, "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } if ( flock($out, LOCK_EX) ) { for( $index = 0; $index $chunks; $index++ ) { if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) { break; } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($in); @unlink("{$filePath}_{$index}.part"); } flock($out, LOCK_UN); } @fclose($out); $response = [ 'success'=true, 'oldName'=$oldName, 'filePaht'=$uploadPath, 'fileSize'=$data['size'], 'fileSuffixes'=$pathInfo['extension'], 'file_id'=$data['id'], ]; die(json_encode($response)); } // Return Success JSON-RPC response die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}'); 以上就是本文的全部内容,希望对大家的学习有所帮助。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。