簡體   English   中英

用PHP顯示最近從遠程FTP服務器上傳的圖像

[英]Displaying recently uploaded images from remote FTP server in PHP

最近,我創建了一個上傳表單,用戶可以在其中將他們的文件上傳到遠程 FTP 服務器。 到現在為止,一切都很順利。 但是,我有一個問題。

我想確保當用戶將他的圖像上傳到遠程 FTP 服務器時,該圖像將立即顯示在網站上。 我怎樣才能做到這一點? 這個問題在 Stack Overflow 上被問了很多。 然而還是有區別的。 在大多數情況下,個人希望從遠程 FTP 服務器下載特定文件。 這不是我的情況。 我想確保用戶看到他上傳的文件顯示在網站上。

我用於將文件上傳到遠程 FTP 服務器的 php 代碼:

<?php
if ( empty( $_FILES['file'] ) ) {
?>
<html>
<head>

</head>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<br>
<input name="submit" type="submit" value="Upload uw album" />
</form>
</body>
</html>
<?php
return;
} else {
?>
<html>
<head>
</head>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<br>
<input name="submit" type="submit" value="Upload uw album" />
</form>
</body>
</html>
<?php
}

$ftp_server = "myserver";
$ftp_user_name = "myuser";
$ftp_user_pass = "mypass";
$source_file = $_FILES['file']['tmp_name'];
$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($conn_id, true); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "Het spijt ons, er is momenteel geen connectie met de server.";
    //echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
     //echo "upload is gelukt";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) { 
//
} else {
?>
<meta http-equiv="refresh" content="0; url=https://radioprogrammabank.nl/wp/upload-album/?name=<?php echo urlencode(basename($_FILES['file']['name']));?>">
<?php
echo "<a href=\"$source_file?file=".urlencode($source_file)."\">".htmlspecialchars($source_file)."</a>";
header('Content-Type: application/octet-stream');
echo file_get_contents('ftp://username:password@ftp.example.com/path/' . $_GET["file"]);

echo "upload is gelukt";
}

// close the FTP stream 
ftp_close($conn_id);
?>

FTP 上傳成功后,您需要將用戶的瀏覽器重定向到查看器頁面。

header("Location: view.php?name=".urlencode(basename($_FILES['file']['name'])));

view.php ,使用示例中的代碼“從遠程 FTP 服務器下載特定文件” 或查看List 並從 FTP 下載單擊的文件

請注意,要使Location標題起作用,您之前不能輸出任何內容——因此沒有echo和 HTML 代碼。


但是由於您沒有獨立的 PHP 代碼,而是從某個 WordPress 插件中執行它,因此 WordPress 甚至在您的代碼開始之前就已經輸出了一些 HTML 標頭。 所以重定向將不起作用。

在 WordPress 開發 Stack Exchange 上有一個問題:
wp_redirect() 在 Insert PHP plugin in WordPress 中不起作用
你問了另一個人:
錯誤:“無法修改標題信息”

或者作為一個蹩腳的黑客,您可以嘗試元重定向:

<meta http-equiv="refresh" content="0; url=view.php?name=<?php echo urlencode(basename($_FILES['file']['name']));?>">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM