PHP文件上传到FTP(此处使用Uploadify作为FTP)--无法执行上传


Here is my HTML Code :
<html>
<head>
<title>Welcome</title>
</head>
<body>
<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>
</body>
</html>

下面是PHP:

<?php
$ftp_server = "94.xx.1.xxx";
$ftp_username   = "anxxxxxx";
$ftp_password   =  "xxxxxxxxx";
$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_servern";
}
else {
echo "could not connect as $ftp_usernamen";
}
$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];
$remote_file_path = "ansxxxx@94.xx.1.xxx/JustForTest".$file; // This is the Folder which I've created inside the FTP 
$remote_file_path2 = "ansxxxx@94.xx.1.xxx/JustForTest".$file2; // This is the Folder which I've created inside the FTP 
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 "nnconnection closed";
?>

错误:

连接为anshxxx@94.xx.1.xxx致命错误:未捕获值错误:路径在C:\examplep\htdocs\upload.php中不能为空:22堆栈跟踪:#0 C:\examplep\tdocs\upload.php(22(:ftp_put(对象(ftp\Connection(,'anshxxx@94.xx...','',1(在第22行的C:\examplep\htdocs\upload.php中抛出#1{main}

它完美连接。。。但没有上传任何文件,引发了上述错误。我是php的新手。请帮忙。。。!

如果有人做过这样的要求,我更愿意分享代码。

提前感谢…!">

尝试以下代码。请在代码中重新定义您的上传目录。我给你的问题写了一条评论,说你应该使用安全的目录名。我不会使用@和。在目录名中。尤其是没有圆点。

<?php
// Build FTP connection and login
function getFtpConnection()
{
$ftp_server = "94.xx.1.xxx";
$ftp_username   = "anxxxxxx";
$ftp_password   =  "xxxxxxxxx";

$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_servern";
return $conn_id;
}
else {
echo "could not connect as $ftp_usernamen";
return false;
}
}
$messages = [];
if(gettype($conn) == "resource")
{
$remoteDirectory = "/html/myuploadDir/";//Absoulte path
foreach($_FILES as $k => $upload_file)
{
if(!empty($upload_file["name"]) && filesize($upload_file["tmp_name"]) > 0)
{
$fname = $upload_file["name"];
$remote_filepath = $remoteDirectory . $fname;
if(ftp_put($conn, $remote_filepath, $upload_file["tmp_name"]))
$messages[] = "File $fname uploaded successfully";
else
$messages[] = "Upload for file $fname failed";
}
}
ftp_close($conn);
$messages[] = "Connection closed";
}
echo implode("<br>", $messages);

最新更新