1

在我通过服务器上的 php 中的 ftp_put 上传任何文件后,文件被上传,但文件在服务器上被损坏。

以下代码用于上传文件。

<html>
<body>

    <form action="upload.php" method="post"
          enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="submit" value="Submit">
    </form>

</body>
</html>

<?php
if (isset($_REQUEST['submit'])) {
    $local_file = $_FILES["file"]["tmp_name"];
    $ftp_path = 'path/newfolder';
    $filename = $_FILES["file"]["name"];
    $usr = 'userid';
    $pwd = 'password';
    $host = 'server_ip';
    $conn_id = ftp_connect($host, 21) or die("Cannot connect to host");
    ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
    $upload = ftp_put($conn_id, $ftp_path . '/' . $filename, $local_file, FTP_BINARY);
    print (!$upload) ? 'Cannot upload' : 'Upload complete';
}
}
?>

我尝试了 jpeg、xls 等...但上传后我无法打开这些文件。

4

1 回答 1

0

不确定是什么可能导致该问题,但假设您需要尝试上传文件后move_uploaded_file

$filename = $_FILES["file"]["name"];    
$local_file = "some/tmp/storage/path/$filename";
if(move_uploaded_file($_FILES["file"]["tmp_name"], $local_file)) {
   $ftp_path = 'path/newfolder';
   $usr = 'userid';
   $pwd = 'password';
   $host = 'server_ip';
   $conn_id = ftp_connect($host, 21) or die("Cannot connect to host");
   ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
   $upload = ftp_put($conn_id, $ftp_path . '/' . $filename, $local_file, FTP_BINARY);
   print (!$upload) ? 'Cannot upload' : 'Upload complete';
   ulink($local_file); //remove temporary local file
} else {
   echo 'File upload error';
}

此外,通过检查确保文件真正上传$_FILES["file"]["error"]。它应该等于UPLOAD_ERR_OK(其他错误代码

于 2013-04-08T09:53:08.613 回答