3

I am trying to do a form that upload a few fields, and 3 images. The images will be uploaded via FTP, there is a problem connecting to ftp with ftp_connect() when the php is uploaded to remote server. It couldn't connect to server when the php is in the shared host. But when the php is in the localhost, it display no error. I am now trying to make this work on Localhost and ftp_put the images into the remote server first, to isolate the error. At the moment i am getting this error.

Warning: ftp_put() expects parameter 1 to be resource, boolean given in line 69

Which is this 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....");
}


?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Benyaman
  • 451
  • 2
  • 10
  • 25

1 Answers1

3

ftp_put() requires a valid resource which is obtained by using ftp_connect(). If you are seeing this error message your call to ftp_connect() failed. You need to obtain a valid resource with ftp_connect(), and if you can't, you need to figure out what is causing your script not to connect to the remote host as that is the root cause of your problem.

To prevent your script from failing you should wrap your attempt to FTP in an if statement that checks to see if ftp_connect() connected successfully:

if (($resource = ftp_connect($host)) !== false) {
    // do FTP stuff
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I tried to replace the $login, with $connect from the ftpconnect php( i just added in the question above), It gives me another error Warning: ftp_put(): Can't open that file: No such file or directory in, not sure how i should fix this problem – Benyaman Nov 28 '13 at 04:50
  • Thanks!I think i managed to upload it onto the remote server, just a problem that it doesn't display the picture when i type in the url. But i will try to figure it out. Thanks for the help! – Benyaman Nov 28 '13 at 05:16