【发布时间】:2022-06-17 22:17:07
【问题描述】:
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_server\n";
}
else {
echo "could not connect as $ftp_username\n";
}
$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 "\n\nconnection closed";
?>
错误:
连接为 anshxxx@94.xx.1.xxx 致命错误:未捕获的 ValueError:C:\xampp\htdocs\upload.php:22 中的路径不能为空 堆栈跟踪:#0 C:\xampp\htdocs\upload.php(22): ftp_put(Object(FTP\Connection) , 'anshxxx@94.xx...', '', 1) #1 {main} 在第 22 行的 C:\xampp\htdocs\upload.php 中抛出
它连接完美...但没有文件上传,抛出上述错误。我是 php 新手。请帮忙...!
如果有人做过这种要求,我更愿意分享代码。
非常感谢...!`
【问题讨论】:
-
做一些调试,比如检查
$_FILES["uploadedfile_1"]["tmp_name"]和$_FILES["uploadedfile_2"]["tmp_name"]实际包含的内容。做一个var_dump($_FILES)并检查。如果它们为空,请检查error是否为0以外的任何内容(这表示上传错误,例如文件太大或其他问题)。永远不要假设两个文件都已成功上传,始终验证和验证您获得的数据。 -
为什么在上传文件时使用
FTP_ASCII而不是默认的FTP_BINARY?会一直是文本文件吗? -
试过没用。
-
我相信您的远程文件路径有问题。您不应该在路径中使用伪目录名称,例如 username@server.ip 等。您必须确定 FTP 服务器上是否允许使用此类目录名称。还有一个问题,你确定你测试的时候上传2个文件吗?如果您只上传一个文件,则出现此错误是正常的。因为第二个文件的上传路径为空。
-
我发布了一个解决方案,如果是这种情况,请尝试并发布任何错误消息。
标签: php html xampp connection uploadify