0

我只是希望在我使用表单文件上传上传图像后,它将在下一页显示这些图像。图像被上传到 ftp 上的文件夹。我到处找,但似乎找不到正确的解决方案。

这是上传图片 (image.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 代码 (upload.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 上回显这两个图像。这段代码有没有办法预测上传了哪些图像?

任何帮助将非常感激。

4

3 回答 3

0

从您的用户上传 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' />";
于 2013-11-05T19:10:53.667 回答
0

这是您可以尝试的完整脚本。

当用户在 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>
于 2013-11-05T23:46:25.170 回答
0
<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>
于 2015-11-06T07:57:42.243 回答