473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using FTP via PHP

133 New Member
Hi,

I am trying to upload some files using ftp via php.

I have built a function which should work as far as i can see but it isn't. Can anyone see why this would not work?


[PHP]
function ftpupload($file, $destination){

// set up the settings
$ftp_server = "ftp.".$_SESSION['domain'];
$ftpuser = $_SESSION['user'];
$ftppass = $_SESSION['pass'];

// delete the below variables if you decide to use a form.
$source_file = $file;
$destination_file = $destination;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY); // the ftp function

// close the connection
ftp_close($conn_id);

}
[/PHP]

Then:

[PHP]
ftpupload($rand_name.$file_ext, "/uploads/images/thumbs/");
[/PHP]

Any Ideas?
Cheers,
Adam
Nov 7 '08 #1
7 3802
code green
1,726 Recognized Expert Top Contributor
[PHP]$ftp_server = "ftp.".$_SESSION['domain']; [/PHP] You don't need the 'ftp' bit for [PHP]ftp_connect($ftp_server); [/PHP]Just the address.
Whether you use the IP address or the hostname is trial and error.
It depends on the ftp set up
Nov 7 '08 #2
adamjblakey
133 New Member
Thank you for your reply,

I have tried using the IP but this did not work either. I have changed the script a bit now to see where it is failing and it seems to connect fine to the FTP. The problem is with sending the file.

[PHP]
function ftpupload($file, $destination){

// set up the settings
$ftp_server = "ftp.".$_SESSION['domain'];
$ftpuser = $_SESSION['user'];
$ftppass = $_SESSION['pass'];

// delete the below variables if you decide to use a form.
$source_file = $file;
$destination_file = $destination;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

//Check if login was successful
if (!$login_result){
echo "Sorry there is currently a problem with our servers, please notify use of this problem or try again later.";
exit();
}

$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // the ftp function

// check upload status
if (!$upload) {
echo "FTP upload has failed!";
exit();
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
exit();
}

// close the connection
ftp_close($conn_id);

}
[/PHP]

I keep getting this erro: FTP upload has failed!
Cheers,
Adam
Nov 7 '08 #3
code green
1,726 Recognized Expert Top Contributor
I would suggest incorrect file path to either $file, $destination.
Echo these values to see what is in there and check the source and remote directories exist.
Be aware the ftp address is a specific folder and all paths are relative to that folder
Nov 7 '08 #4
adamjblakey
133 New Member
Right i have given up doing it that way, i have however found a way that does upload but when the file is uploaded on the server it is 0kb but at least it is uploading.

[PHP]
function ftpupload($file){

$ch = curl_init();
$localfile = $file;
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://'.$_SESSION['name'].':'.$_SESSION['pass'].'@87.229.14.87/httpdocs/uploads/images/thumbs/'.$file);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
print 'File uploaded succesfully.';
} else {
print 'File upload error.';
}

}
[/PHP]

Any Ideas why it is 0kb?
Nov 7 '08 #5
code green
1,726 Recognized Expert Top Contributor
Could be a few things.
File space full.
Blocked by firewall.
Corrupted file
Not really hph problem.
You original function indicated a file path problem.
Did you investigate this?
Nov 7 '08 #6
adamjblakey
133 New Member
Thank you for your reply, the disk space is not the problem as i have check this and it is fine. I don't have a firewall on so this cannot be the problem either. The file is not corrupt either.

I did investigate this but i could not find out the problem even after hours of looking at it, and was feeling like i was going in circles.

Cheers,
Adam
Nov 8 '08 #7
adamjblakey
133 New Member
Hi,

I have come up with the solution to this now but have a little problem.

I am now using this to upload the images.

[PHP]
function ftpupload($file, $img_tmp, $path){

$message = '';
if ($ftp = ftp_connect($_SESSION['ddomain'])) {
if (ftp_login($ftp, $_SESSION['dname'], $_SESSION['dpass'])) {
ftp_pasv($ftp, true);
if (ftp_put($ftp, $path . $file,
$img_tmp, FTP_BINARY)) {
$message = "File uploaded";
} else {
die("Could not upload file");
}
} else {
die("Could not login to FTP account");
}
} else {
die("Could not connect to FTP server");
}
}
[/PHP]

Now if i pass an image to this via a form it is fine, the problem i am having is i am running this function with another function which resizes images and renames it but the ftp function relies on an input of ['name'] and ['tmp_name'] to upload the image.

This is the function for image rename and resize:

[PHP]
function uploadimage($value){
$file_type = $value['type'];
$file_name = $value['name'];
$file_size = $value['size'];
$file_tmp = $value['tmp_name'];

//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}

//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = 175;
$ImgWidth = 700;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
$newwidth2 = $ImgWidth;
$newheight2 = $ImgWidth/$imgratio;

}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;

$newheight2 = $ImgWidth;
$newwidth2 = $ImgWidth*$imgratio;
}


$resized_img = imagecreatetruecolor($newwidth, $newheight);
$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);

//save image
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");

imagedestroy($resized_img);
imagedestroy($resized_img2);
imagedestroy($new_img);


}
$value = "$rand_name.$file_ext";
return $value;
}
[/PHP]

This works fine as it is now as i am just passing the real image name but i don't know how to do it with the renamed image.

Any ideas?
Cheers,
Adam
Nov 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.