我已经尝试使用php中的ftp连接将文件上传到服务器并且它无法正常工作,它的连接和告诉上传成功但是没有图像将在目录中上传....我试过以下代码请帮助纠正它
image.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>
upload.php的
<?php
$ftp_server = "XXXXXX";
$ftp_username = "XXXXX";
$ftp_password = "XXXX";
$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 = "/imagetest/123/".$file;
$remote_file_path2 = "/imagetest/123/".$file2;
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";
?>
答案 0 :(得分:0)
尝试..
$remote_file_path = "./imagetest/123/".$file;
或者只是......
$remote_file_path = "imagetest/123/".$file;
试试这个......
$uploaddir = "./temp/";
$uploadfile = $uploaddir . basename($_FILES['uploadedfile_1']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['uploadedfile_1']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
答案 1 :(得分:0)
$_FILES["uploadedfile_1"]["tmp_name"]
只是一个包含临时文件名的字符串(例如&#39; aaaaa.jpg&#39;但不是&#39; /tmp/upload/aaa.jpg')。您应该使用move_uploaded_file
移动使用表单上传的文件,因为$_FILES["uploadedfile_1"]["tmp_name"]
不包含实际的文件路径。
http://php.net/manual/en/function.move-uploaded-file.php
由于我没有经验将上传的文件直接转移到另一台服务器,我想这里有什么可行的。 1.首先将上传的文件移动到本地文件夹。
$localPath = '/xxxx/upload/' //confirm your web server user have the right to write in this folder
$tmp_name = $_FILES["uploadedfile_1"]["tmp_name"] // confirm you got a filename here.
move_uploaded_file($tmp_name, "$localPath/$tmp_name"); // You should have a new file in /xxxx/upload/
在您的网络服务器中。
2.然后将文件从本地文件夹移动到远程服务器。
ftp_put($conn_id, $remote_file_path, "$localPath/$tmp_name" ,FTP_ASCII);// If this goes wrong, you should test this with a local file created mannually and upload it to your remote server. If it fails, there must be something wrong in your remote connection or permission.
3.然后删除本地文件夹中的文件。
unlink("$localPath/$tmp_name");// The file in local host should be deleted.
请试一试。
答案 2 :(得分:0)
首先,确保文件的路径正常(语法上并且实际上指向已启动的文件);其次,ftp_put
函数返回boolean
。您可以检查它的值,看看上传是否成功。
我认为在这种情况下的另一个问题可能是服务器的防火墙阻碍了在客户端(即你)和服务器之间建立数据通道。尝试在上传文件之前打开passive mode
。只需在ftp_put
之前添加以下行,看看它是否有效:
// turn passive mode on
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
您还必须使用二进制模式正确指出图像。
Relevant documentation。希望它能让你开始朝着正确的方向前进。