我正在为网站做图片库,但管理员希望自己上传图片,所以我准备了这段代码将图片从网站传输到FTP。
但有些人告诉你必须将图像路径存储在数据库中,然后你必须从mysql中获取它并在网站上显示/删除/上传图像。
由于我是PHP中的新蜜蜂,我无法做到这一点,所以任何人都可以帮助我解决这个问题,... Thanx提前
<!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 action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" /><br><br>
<input name="file1" type="file" /><br><br>
<input name="file2" type="file" /><br><br>
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php的
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$ftp_server = "xxxxxxxxxxxxxx";
$ftp_user_name = "xxxxxxxxxxx";
$ftp_user_pass = "xxxxxxxxx";
$folder = "/www/imagetest/123/";
$source_file = $_FILES['file']['tmp_name'];
$source_file1 = $_FILES['file1']['tmp_name'];
$source_file2 = $_FILES['file2']['tmp_name'];
$destination_file = $folder . $_FILES['file']['name'];
$destination_file1 = $folder . $_FILES['file1']['name'];
$destination_file2 = $folder . $_FILES['file2']['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);
$upload1 = ftp_put($conn_id, $destination_file1, $source_file1, FTP_BINARY);
$upload2 = ftp_put($conn_id, $destination_file2, $source_file2, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload 1 has failed!\n";
} else {
echo "1 Uploaded $source_file to $ftp_server as $destination_file\n";
}
if (!$upload1) {
echo "FTP upload 2 has failed!\n";
} else {
echo "2 Uploaded $source_file to $ftp_server as $destination_file\n";
}
if (!$upload2) {
echo "FTP upload 3 has failed!\n";
} else {
echo "3 Uploaded $source_file to $ftp_server as $destination_file\n";
}
// close the FTP stream
ftp_close($conn_id);
?>