通过php代码错误将图像上传到FTP

时间:2016-07-29 08:09:14

标签: php ftp

我的图像上传有问题,它不起作用...... 给我这个错误

  

连接到ftp.exemple.com,用户exemple@exemple.com警告:   ftp_put():文件名不能为空   第26行的/home/user/public_html/foto-test.php FTP上传有   失败!

<?php
$ftp_server = "ftp.xxxx.com";
$ftp_user_name = "xxxx@xxxx.com";
$ftp_user_pass = "xxxxxxx";
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILE['file']['tmp_name'];

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// 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); //line 26

// 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);
?>

我尝试更改$ folder目标但是它给了我一些错误......

1 个答案:

答案 0 :(得分:1)

在此处查看答案PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in

此外,您的目标路径最后应有斜杠

$folder = "/public_html/images"; 
$destination_file = $folder . $_FILES['file']['name'];

将其更改为

$folder = "/public_html/images/"; $destination_file = $folder . $_FILES['file']['name'];

所以很明显,你上传的任何(图片)进入图片文件夹而不是'image + SomeName'文件夹