【问题标题】:PHP - Upload images to FTP directory and then Display images uploaded only from directoryPHP - 将图像上传到 FTP 目录,然后显示仅从目录上传的图像
【发布时间】:2013-11-05 18:26:06
【问题描述】:

我只是希望在我使用表单文件上传上传图像后,它将在下一页显示这些图像。图像被上传到 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 上回显这两个图像。这段代码有没有办法预测上传了哪些图片?

任何帮助将不胜感激。

【问题讨论】:

    标签: php image forms ftp


    【解决方案1】:

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

    当用户在 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】:

      从您的用户上传 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' />";
      

      【讨论】:

      • 我试过了,但没有用。我想我做错了什么。无法在此处发布我的代码,因为它的限制字符。无论如何你可以为我结合php。另外,我对“/a/writable/path/on/your/system/”感到困惑。我应该把什么路径放在这里?
      • 好的,我在另一个答案中为你写了一个完整的脚本。当然,这些只是示例。
      【解决方案3】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多