Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

html - Displaying recently uploaded images from remote FTP server in PHP

Recently, I have created an upload form in which users can upload their files to a remote FTP server. Until now, everything is going well. However, I have a problem.

I want to make sure that when the user his image is uploaded to the remote FTP server, the image will be displayed immediately on the website. How can I do this? This question has been asked a lot on Stack Overflow. Yet there is a difference. In most cases, the individual wanted to download a specific file from the remote FTP server. This is not the case with me. I want to make sure that the user sees the file he uploaded displayed on the site.

My php code for uploading a file to the remote FTP server:

<?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:[email protected]/path/' . $_GET["file"]);

echo "upload is gelukt";
}

// close the FTP stream 
ftp_close($conn_id);
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Once the FTP upload succeeds, you need to redirect user's browser to a viewer page.

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

In the view.php, use the code from your examples to "download a specific file from the remote FTP server". Or see List and download clicked file from FTP.

Note that for the Location header to work, you cannot output anything before – so no echo and no HTML code.


But as you do not have a standalone PHP code, but you executed it from within some WordPress plugin, the WordPress will have already outputted some HTML headers before your code even starts. So the redirect won't work.

There's a question on this on the WordPress development Stack Exchange:
wp_redirect() not working in Insert PHP plugin in WordPress.
And you have asked another one:
Error: “Cannot modify header information”

Or as a lame hack, you can try meta redirect:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...