Page 1 of 2

FTP Code Problem

Posted: Fri Oct 08, 2004 8:22 am
by ffcyan
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color] 

I have the following code, which works as far as connecting to the FTP site but doesn't upload the file. When the code runs, this is the message that is received:

Connected to ftp.transparentvalue.com, for user user. FTP upload has failed!

I can't figure out for the life of me why this won't work. All the code seems to be in order. If someone could help me, it would mean the world to me. Thank you.

Code: Select all

<?php

// set up basic connection
$ftp_server = 'ftp.transparentvalue.com';
$conn_id = ftp_connect($ftp_server); 
$ftp_user_name = 'user';
$ftp_user_pass = 'pw';
$destination_file = 'somefile.txt';
$source_file = 'somefile.txt';


// 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); 

// check upload status
if (!$upload) 
{echo "FTP upload has failed!";}
	   
else

{echo "Uploaded $source_file to $ftp_server as $destination_file";}

// close the FTP stream 
ftp_close($conn_id);

?>

Self Note

Posted: Fri Oct 08, 2004 8:46 am
by ffcyan
Perhaps it has to do with the file paths?
Or maybe it is a server-side problem? (i.e. does not allow external FTP -
I use Dreamweaver right now to FTP my files directly)

connecting

Posted: Fri Oct 08, 2004 9:09 am
by phpScott
the fact that you are connecting will dismiss the idea of serve configurations and the fact that you use dreamweaver to ftp sites now.

is 'somefile.txt' in the same directory as your script, try putting the full path ie.
c:\some\directory\path\somefile.txt

and see if it works.

on the upload side it should just put it into your web directory if you don't include a file path, you might have to do some directory changes, can't remeber to long since made ftp class.

Reply

Posted: Fri Oct 08, 2004 9:32 am
by ffcyan
I have the .php file and somefile.txt in the same directory. Even so, I tried the full path and still no luck. Maybe I have the wrong syntax in place for the root directory.

Posted: Fri Oct 08, 2004 9:39 am
by infolock
well, i'll tell you what the most likely problem is. you aren't setting the directory into which FTP will look to upload files from. ie, it's probably looking in the default directory for somefile.txt (ie, c:\windows\desktop\). i would set the default directory to be wherever you are trying to upload from, as well as set the directory into which you are trying to upload TO as you may be trying to upload to the root directory without the correct privelages.

if all else fails, change your error messages from FTP Upload failed 1 for the first fail, FTP Upload Failed 2! for the second error and so on to pinpoint just where exactly your problem exists...

Reply

Posted: Fri Oct 08, 2004 10:31 am
by ffcyan
I changed the source directory to exactly where the file is. I also changed the destination directory to where I want the file. Maybe the syntax '.../...somefile.txt' is incorrect?

Posted: Fri Oct 08, 2004 10:53 am
by feyd
it should probably be a full path on the destination.

Reply

Posted: Fri Oct 08, 2004 11:27 am
by ffcyan
Perhaps, but then what is the correct syntax for uploading to the root directory?

Posted: Fri Oct 08, 2004 11:37 am
by feyd
the root path of the site.

on most hosts it's similar to:

/home/<yourusername>/www/

Reply

Posted: Fri Oct 08, 2004 3:26 pm
by ffcyan
I think I've narrowed down the problem. It has to do with the source path. For example, the file somefile.txt is in:

C:\Documents and Settings\Julian\My Documents\TV\somefile.txt

I have tried using this path, but no luck. There must be additional syntax that I am missing.

Posted: Fri Oct 08, 2004 4:16 pm
by hedge
Just a shot in the dark here but no one's mentioned it yet.... I apologize if this is obvious.

You aren't trying to upload from a client machine via the server are you, of course that won't work, the file has to be on the server.

Posted: Sat Oct 09, 2004 1:30 am
by m3mn0n
Indeed.


Some helpful research links:
[big_search]php ftp script[/big_search]

Reply

Posted: Sat Oct 09, 2004 8:23 am
by ffcyan
I am trying to upload a file from my hard drive to a server, that's the point of an FTP program after all. It's just as simple as using an FTP program (say in Dreamweaver) that takes a file saved on one's hard drive and places it on a server. There has to be a way to do this...

Posted: Sat Oct 09, 2004 8:53 am
by Bill H
The following uploads an image from local hard drive to the server. The $_SESSION var is the server subdirectore assigned to the logged in user, so just replace it with your desired server folder name.

Code: Select all

<?php                                   // Presents the selected member's images for viewing
session_cache_limiter("private,must-revalidate");             // latest revision May 25, 2003
session_start();
ob_start();

if (isset($MAX_FILE_SIZE))
{
     if (is_uploaded_file($_FILES['Ifile']['tmp_name']))
     {
          $chk = getimagesize($_FILES['Ifile']['tmp_name']);
          if ($chk == NULL)
               $form = $_FILES['Ifile']['name'] . " is not a valid image.";
          else if ($chk[2] > 2)
               $form = $_FILES['Ifile']['name'] . " is not a gif/jpg image.";
          else if ($chk[0] > 600)
               $form = $_FILES['Ifile']['name'] . " is too wide to upload.";
          else if ($chk[1] > 400)
               $form = $_FILES['Ifile']['name'] . " is too high to upload.";
          else
          {    $Dest = "files/img" . $_SESSION['vcode'] . $_FILES['Ifile']['name'];
               if (move_uploaded_file($_FILES['Ifile']['tmp_name'], $Dest) != FALSE)
               {
                    $lth = strlen($_FILES['Ifile']['name']) - 3;
                    $foo = substr($_FILES['Ifile']['name'], 0, $lth);
                    $Dest = "files/dat" . $_SESSION['vcode'] . $foo . "dat";
                    $f = fopen($Dest, "w");                                // for writing, truncated
                    fwrite($f, $_POST['Text'] . "|" . $_POST['Title']);
                    fclose($f);
                    header("Location:imglist.php");
                    exit();
               }
               else $form = "File write failed: " . $_FILES['Ifile']['name'];
          }
     }
     else $form = "File upload failed: " . $_FILES['Ifile']['name'];

     $Inform = "<span style='color: RGB(220, 0, 0)'>" . $form . "</span>";
}
else $Inform = $_SESSION['uname'] . " is uploading a new image file.";

ob_end_flush();

?>
<form enctype="multipart/form-data" action="addimg.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="200000">
<table>
<tr><td colspan=2 valign=bottom><H2>File:&nbsp&nbsp&nbsp
<input name="Ifile" type="file"><br><br></td></tr>

<tr><td colspan=2><H5>Click the "Send" button only once and be patient.<br>
It may take some time for the image to be uploaded.</td><tr>

<tr><td><H5><br><input type="image" src="images/send.gif" name="Send">
</td></form>
<td align=right><H5><br>
<a href="imglist.php"><img src="images/cancel.gif" width=68 height=21 border=0></a>
</td></tr></table>

</center>
</body>
</html>

Reply

Posted: Sat Oct 09, 2004 9:16 am
by ffcyan
I appreciate the code you have posted, but that is not what I want to do. I want to do what I previously mentioned. No user is logged onto the server in my scenario. Also, my files will XML files. Therefore your code would not be appropriate. I just need to know what the syntax is for locating a local file. Thank you.