ftp_put() 函数不会上传我的文件

我已经在谷歌上搜索过这个错误,并从这个网站上找到了很多帖子,但似乎没有任何效果。这是我的来自:

<form enctype="multipart/form-data" action="upload_file.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
</form>

这是来自upload_file.php的我的php代码

<?php
$ftp_server = "xxx";
$ftp_username   = "xxx";
$ftp_password   =  "xxx";
$uploadedfile = $_FILES["uploadedfile"]["tmp_name"];
//setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
//login
if(@ftp_login($conn_id, $ftp_username, $ftp_password))
{
  echo "conectd as $ftp_username@$ftp_servern";
}
else
{
  echo "could not connect as $ftp_usernamen";
}
ftp_pasv($conn_id, true);
$remote_file_path = "   /home/a9408563/public_html/files".$uploadedfile;
ftp_put($conn_id, $remote_file_path, $uploadedfile,FTP_BINARY);
ftp_close($conn_id);
echo "nnconnection closed";
?>

这是我得到的错误:警告:ftp_put()[function.ftp-put]:无法打开该文件:第18行的/home/a9408563/public_html/upload_file.php中没有这样的文件或目录

我不知道下一步该做什么,任何帮助将不胜感激!

stack overflow ftp_put() function will not upload my file
原文答案

答案:

作者头像

这是 php.net 在 ftp_put 上的评论的答案:


找到了问题,你不能把路径放到目标文件中(即使我可以在 dos ftp 客户端中做到这一点......?)

例如- 这不起作用

ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);

你必须把

ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );

http://php.net/manual/en/function.ftp-put.php

作者头像
<?php
    if(isset($_POST["submit"])){
// set up basic connection
$conn_id = ftp_connect("xxx"); 
$ftp_user_name="xxx";
$ftp_user_pass="xxx";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
$destination_file =  $destination_file = "public_html/api/".$_FILES['uploadedfile']['name'];
$source_file = $_FILES['uploadedfile']['tmp_name']; 
// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!";
} else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream 
ftp_close($conn_id); 
    }
?>
<html><body><form action="upload.php" enctype="multipart/form-data" method="post">
<input name="uploadedfile" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form></body></html>

api is my target folder