Uploading Files to FTP using PHP

I am unable to upload files to my FTP using PHP.

Page 1 of 1

2 Replies - 2894 Views - Last Post: 02 March 2010 - 08:01 AM Rate Topic: -----

#1 sajanm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 10

Uploading Files to FTP using PHP

Posted 02 March 2010 - 04:04 AM

I tried uploading files to my ftp folder vis PHP and have found no positive results.I keep getting errors regarding this.Any help??

Script to upload a file via FTP

Create a file named "upload.html"

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%">server name </td>
<td width="75%"><input name="server" type="text" id="server">Write in the format "ftp.servername.com" </td>
<td width="11%">&nbsp;</td>
</tr>
<tr>
<td>user name </td>
<td><input name="username" type="text" id="username"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>password</td>
<td><input name="password" type="password" id="password"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>File name </td>
<td><input type="file" name="file"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Upload"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>



Now create a file named "upload.php"

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$ftp_server=$_POST['server'];
$ftp_user_name=$_POST['username'];
$ftp_user_pass=$_POST['password'];
$source_file=$_FILES['file']['name'];// retrieve name of the file to be uploaded
$destination_file=$source_file;
// make a connection to the ftp server
$conn_id = ftp_connect($ftp_server);

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

// 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);
?>
</body>
</html>

This post has been edited by JackOfAllTrades: 02 March 2010 - 06:28 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Uploading Files to FTP using PHP

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Uploading Files to FTP using PHP

Posted 02 March 2010 - 06:28 AM

Here's a radical idea: SHARE THE ERRORS YOU ARE RECEIVING!!!
Was This Post Helpful? 0
  • +
  • -

#3 sajanm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-March 10

Re: Uploading Files to FTP using PHP

Posted 02 March 2010 - 08:01 AM

well i recieve the errors such as:

FTP connection has failed!Attempted to connect to ftp.heavyequipmentsonline.freehostia.com for user sajmah3

actually i am using a freehosting site and i even confirmed with them whether they allowed PHP to add files using FTP and i got a positive repply... but i cant understand why my file doesnt get uploaded and gives me the following error...

i have even uploaded the code for trial.

http://heavyequipmen....com/upload.php

Since this can be used to upload files to any ftp server.. if someone with access to an FT can try it will be a boon...

And if someone who has already programmed something like this can share mw their code..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1