我正在尝试上传一些字段和3张图片的表单。图像将通过FTP上传,当php上传到远程服务器时,使用ftp_connect()连接到ftp时出现问题。当php在共享主机中时,它无法连接到服务器。但是当php在localhost中时,它没有显示任何错误。我现在正尝试将此工作在Localhost上,并首先将图像ftp_put输入远程服务器,以隔离错误。 目前我收到了这个错误。
Warning: ftp_put() expects parameter 1 to be resource, boolean given in line 69
这是ftp_put($login,"abc.info/$photo",$_FILES["file"]["tmp_name"][$i],FTP_ASCII)
<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';
include 'Agentftpconnect.php';
if(!isset($_SESSION['username']))
{
header("Location: home.php");
exit;
}
if ($_POST)
{
//get form data
$Listingname = addslashes(strip_tags($_POST['Listingname']));
$Location = addslashes(strip_tags($_POST['Location']));
$nobed = addslashes(strip_tags($_POST['nobed']));
$zip = addslashes(strip_tags($_POST['zip']));
$price = ($_POST['price']);
if (!$Listingname||!$nobed||!$Location||!$zip||!$price)
die ("Please fill out all fields");
else
for($i=0;$i<3;$i++)
{
if ((($_FILES["file"]["type"][$i] !== "image/gif")
|| ($_FILES["file"]["type"][$i] !== "image/jpeg")
|| ($_FILES["file"]["type"][$i] !== "image/jpg")
|| ($_FILES["file"]["type"][$i] !== "image/pjpeg")
|| ($_FILES["file"]["type"][$i] !== "image/x-png")
|| ($_FILES["file"]["type"][$i] !== "image/png"))
&& ($_FILES["file"]["size"][$i] > 400000))
die("File is not correct");
else{
if ($_FILES["file"]["error"][$i] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"][$i] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"][$i] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][$i] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"][$i] . "<br>";
echo "<br>";
if (file_exists("abc.info/rent" . $_FILES["file"]["name"][$i]))
{
die($_FILES["file"]["name"][$i] . " already exists please add another file, or change the name ");
}
else
{
$photo=$_FILES["file"]["name"][$i];
ftp_put($login,"abc.info/$photo",$_FILES["file"]["tmp_name"][$i],FTP_ASCII) or die("Can not upload");
echo "Stored in: " . "abc.info/rent/" . $_FILES["file"]["name"][$i];
}
}
}
}
{
$photo0=$_FILES["file"]["name"][0];
$photo1=$_FILES["file"]["name"][1];
$photo2=$_FILES["file"]["name"][2];
$username=$_SESSION['username'];
//register into database
mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1,pic2,pic3) VALUES
('$username','$Listingname','$Location','$nobed','$zip','$price','$photo0','$photo1','$photo2');") or die(mysqli_error());
echo "Listing Added";
}
}
else
{
?>
<form action="Submitlisting8.php" method="post"
enctype="multipart/form-data">
Listing Name:<br />
<input type='text' name='Listingname'><p />
Location:<br />
<input type='text' name='Location'><p />
Number of Beds:<br />
<input type='test' name='nobed'><p />
Zip:<br />
<input type='text' name='zip'><p />
Price:<br />
<input type='text' name='price'><p />
<label for="file">Pic1(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file[]"><br>
<label for="file">Pic2(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file[]"><br>
<label for="file">Pic3(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file[]"><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
<FORM METHOD="LINK" ACTION="agentaccount.php">
<INPUT TYPE="submit" VALUE="Back to Account">
</form>
<?php
}
?>
FTPconnect
<?php
$ftp_user_name='name';
$ftp_user_pass='pass';
$connection = 'ftp.abc.info';
$connect = ftp_connect($connection) or die("Couldn't connect to $connection");
$login=ftp_login($connect, $ftp_user_name, $ftp_user_pass);
if(!$login)
{
die("But failed at login Attempted to connect to $connection for user $ftp_user_name....");
}
?>
答案 0 :(得分:2)
ftp_put()
需要使用ftp_connect()
获得的有效资源。如果您看到此错误消息,则您对ftp_connect()
的调用失败。您需要使用ftp_connect()
获取有效资源,如果不能,则需要弄清楚导致脚本无法连接到远程主机的原因,因为这是导致问题的根本原因。
为防止脚本失败,您应该在if
语句中将尝试包装到FTP,以检查ftp_connect()
是否已成功连接:
if (($resource = ftp_connect($host)) !== false) {
// do FTP stuff
}