PHP - 将图像上传到FTP目录,然后显示仅从目录上传的图像

时间:2013-11-05 18:26:06

标签: php image forms ftp

我只想在使用表单文件上传上传图像后,它会在下一页显示这些图像。图像上传到ftp上的文件夹。我到处寻找,但似乎无法找到正确的解决方案。

这是用于上传图片的HTML代码(image.php):

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

非常感谢任何帮助。

3 个答案:

答案 0 :(得分: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' />";

答案 1 :(得分: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>

答案 2 :(得分: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>