關於php跨域shang傳文件的信息

本文目錄一覽:

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”}’); 以上就是本文的全部內容,希望對大家的學習有所幫助。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285688.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 15:45
下一篇 2024-12-22 16:05

相關推薦

發表回復

登錄後才能評論