我只是希望在我使用表单文件上传上传图像后,它将在下一页上显示这些图像。图像将上传到 ftp 上的文件夹。我到处寻找,但似乎找不到正确的解决方案。
这是上传图片(图片.php)的HTML代码:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile_1" type="file" /><br />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
这是下一页的PHP代码(上传.php):
<?php
$ftp_server = "localhost";
$ftp_username = "xxx";
$ftp_password = "xxx";
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
if(@ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "connected as $ftp_username@$ftp_server'n";
}
else {
echo "could not connect as $ftp_username'n";
}
$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];
$remote_file_path = "/2013/img/".$file;
$remote_file_path2 = "/2013/img/".$file2;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile_1"]["tmp_name"],FTP_ASCII);
ftp_put($conn_id, $remote_file_path2, $_FILES["uploadedfile_2"]["tmp_name"],FTP_ASCII);
ftp_close($conn_id);
echo "'n'nconnection closed";
?>
以上只是上传代码,我不知道我需要使用什么 PHP 代码来显示图像,但通常,我想在 Upload.php 上回显这两个图像。有没有办法让这段代码预测上传了哪些图像?
任何帮助将不胜感激。
这是一个完整的脚本,你可以试试。
当用户在 PHP 上上传某些内容时,该内容会转到具有临时名称的临时目录。因此,您应该将文件从那里移动到所需的位置。这就是脚本中的move_uploaded_file()
的作用。此外,要显示图像,该路径必须可供您的 Web 服务器使用,这意味着在 public_html
目录下。为了使 PHP 能够在上传文件后将文件移动到那里,目录必须是可写的。我添加了一些代码,用于在当前脚本的目录中创建一个名为 uploads
的可写目录。
<html>
<head></head>
<body>
<?php
// The HTML beginning tags must be there or the output won't render
// as a web page
// Local path for storing the files, under the directory of this file
$local_path = dirname(__FILE__) . '/uploads';
// Create the directory if it doesn't exist
if ( ! is_dir( $local_path ) ) {
mkdir( $local_path );
}
// Make the directory writable
if ( ! is_writable( $local_path ) ) {
chmod( $local_path, 0777 );
}
// Loop through the uploaded files
foreach ( $_FILES as $ul_file ) {
// Check for upload errors
if ( $ul_file['error'] )
die( "Your file upload failed with error code " . $ul_file['error'] );
// Set a new file name for the temporary file
$new_file_name = $local_path . '/' . $ul_file['name'];
// Move the temporary file away from the temporary directory
if ( ! move_uploaded_file( $ul_file['tmp_name'], $new_file_name ) )
die( "Failed moving the uploaded file" );
// Store the local file paths to an array
$local_file_paths[] = $new_file_name;
}
// Now do your FTP connection
$ftp_server = "localhost";
$ftp_username = "xxx";
$ftp_password = "xxx";
$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if ( @ftp_login($conn_id, $ftp_username, $ftp_password) ) {
echo "<p>Connected as $ftp_username @ $ftp_server</p>";
} else {
die( "Could not log in as $ftp_username'n" );
}
// Loop through the local filepaths array we created
foreach ( $local_file_paths as $local_file_path ) {
// The remote file path on the FTP server is your string +
// the base name of the local file
$remote_file_path = "2013/img/" . basename( $local_file_path );
// Put the file on the server
ftp_put( $conn_id, $remote_file_path, $local_file_path, FTP_BINARY );
echo "<p>Uploaded a file to $remote_file_path</p>";
}
// Close the connection
ftp_close( $conn_id );
echo "<p>Connection closed. Your images are here:</p>";
// Loop through the file paths again and output an HTML IMG element for each
foreach ( $local_file_paths as $local_file_path ) {
echo "<img src='$local_file_path' alt='Your uploaded file' />";
}
// Final HTML tags
?> </body></html>
从用户上传 HTTP POST 后,文件将转到临时名称下的临时目录。服务器上的实际文件名包含在 $_FILES['uploadedfile_1']['tmp_name']
和 $_FILES['uploadedfile_2']['tmp_name']
中。(['name']
是用户计算机上的原始文件名。然后,您应该检查上传错误,将文件移离临时目录,然后可以通过FTP放置它们。最后,您可以输出一个 HTML IMG 元素。
// Check for errors
if ( $_FILES['uploadedfile_1']['error'] )
die( "Upload failed with error code " . $_FILES['uploadedfile_1']['error'] );
// Set a new path for the file. Use the original file name.
$new_path = '/a/writable/path/on/your/system/' . $_FILES['uploadedfile_1']['name'];
// Move the file away from the temporary directory
if ( ! move_uploaded_file( $_FILES['uploadedfile_1']['tmp_name'], $new_path ) ) {
die( "Failed moving the uploaded file" );
}
// Put the file on the FTP connection established before
// Use the FTP_BINARY type for image files
ftp_put($conn_id, $remote_file_path, $new_path,FTP_BINARY);
// After closing the connection, output an IMG element
// This works if your $new_path is readable by the web server
echo "<img src='$new_path' alt='Your uploaded file' />";
<html>
<head></head>
<body>
<?php
// The HTML beginning tags must be there or the output won't render
// as a web page
if ($_POST) {
// Local path for storing the files, under the directory of this file
$local_path = dirname(__FILE__) . '/uploads';
// Create the directory if it doesn't exist
if (!is_dir($local_path)) {
mkdir($local_path);
}
// Make the directory writable
if (!is_writable($local_path)) {
chmod($local_path, 0777);
}
// Loop through the uploaded files
foreach ($_FILES as $ul_file) {
// Check for upload errors
if ($ul_file['error']) {
die("Your file upload failed with error code " . $ul_file['error']);
}
// Set a new file name for the temporary file
$new_file_name = $local_path . '/' . $ul_file['name'];
// Move the temporary file away from the temporary directory
if (!move_uploaded_file($ul_file['tmp_name'], $new_file_name) ) {
die("Failed moving the uploaded file");
}
// Store the local file paths to an array
$local_file_paths[] = $new_file_name;
}
// Now do your FTP connection
$ftp_server = "server";
$ftp_username = "username";
$ftp_password = "password";
$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (@ftp_login($conn_id, $ftp_username, $ftp_password)) {
echo "<p>Connected as $ftp_username @ $ftp_server</p>";
} else {
die("Could not log in as $ftp_username'n");
}
// Loop through the local filepaths array we created
foreach ($local_file_paths as $local_file_path) {
// The remote file path on the FTP server is your string +
// the base name of the local file
$remote_file_path = basename( $local_file_path );
// Put the file on the server
ftp_put( $conn_id, $remote_file_path, $local_file_path, FTP_BINARY );
echo "<p>Uploaded a file to $remote_file_path</p>";
}
// Close the connection
ftp_close($conn_id);
}
?>
<form enctype="multipart/form-data" action="img_upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile_1" type="file" /><br />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
</body>
</html>