Link to home
Start Free TrialLog in
Avatar of tepee
tepee

asked on

ftp_put problem

Hi guys, been searching through different forums for a while now to try and get this problem sorted but found nothing even after looking on here so I though I should ask and see.

I am writing an image upload web app that eventually will use passed variables to upload an image to the customer hosting account (after a temp file is held on the cm server). I intend to create a few functions which will by then end of the file have uploaded the image to the appropriate place on the customer account and also enter it into the appropriate record in a specified field on a table.

The problem I have is that the ftp upload script I am using gives me the following error:

Warning: ftp_put() [function.ftp-put.php]: Cannot open data connection. in D:\*********\wwwroot\includes\imgupload\process1.php on line 115

(Line 115 is the ftp_put(....) command ) it is listed as line #74 in the code window below

The file uploads fine locally and the thumbnail process works perfectly

I do get a file with the correct name on the new location but the filesize is zero kb.

permission of the new location folder is set to 777


I hope that someone can shed a bit of light on this.

Thanks in advance

Tim
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
 
 
?>
<?php
 
function createthumb($name,$filename,$new_w,$new_h){ 
 
   if (preg_match('/\.(jpg|jpeg)$/',$name)){ 
       $src_img=imagecreatefromjpeg($name); 
   } else if (preg_match('/\.png$/',$name)){ 
       $src_img=imagecreatefrompng($name); 
   } else { 
     die("Issue with thumbnail creation around function call preg match, make sure you didn't upload a .gif file"); 
   }  
   
   $system=explode('.',$name);
 
	
	$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
	$thumb_w=$new_w;
	$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
	$thumb_w=$old_x*($new_w/$old_y);
	$thumb_h=$new_h;
}
if ($old_x == $old_y) {
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}
 
 
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	
	if (preg_match("/png/",$system[1]))
{
	imagepng($dst_img,$filename); 
} else {
	imagejpeg($dst_img,$filename); 
}
imagedestroy($dst_img); 
imagedestroy($src_img); 
 
echo'<br><br><span class="contenttextgreen">Thumbnail created successfully, adding the image to the database...</span>';
 
}
 
 
function ftp_copy($source_file, $destination_file)
{
	
	$ftp_server = 'ftp.********.com';
	$ftp_user = '********.com';
	$ftp_password = '********';
 
	$conn_id = ftp_connect($ftp_server);
	$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
 
	if((!$conn_id) || (!$login_result))
	{
            echo "FTP connection has failed!";
            echo "Attempted to connect to $ftp_server for user $ftp_user";
            
            ftp_pasv($conn_id,true);
            
          
   	}
 
	$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
	ftp_close($conn_id); 
 
	if ($upload === false) {  // check upload status
         echo "<h2>FTP upload  has failed!</h2> <br />";
      } else {
         echo "Uploading Complete!<br /><br />";
      }}
 
 
 
 
 
 
 
 
 
?>
 
 
 
 
 
 
 
</head>
 
 
<div id="wrapper">
 
<?php  include ('includetop.php');  ?>
 
 
<div id="loadwindow">
  
 <?php 
  
  $target = "uploads/"; 
  
$target = $target . basename( $_FILES['imagefile']['name']) ; 
$ok=1; 
 
if(move_uploaded_file($_FILES['imagefile']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['imagefile']['name']). " has been uploaded";
 
$namer = time().basename($_FILES['imagefile']['name']);
 
rename($target,"uploads/".$namer."" );
 
 
 
 
/* Create the thumbnail */
 
createthumb("uploads/".$namer."","uploads/thumbs/tn_".$namer."",300,300);
 
 
 
$imagefile_name = 'tn_'.$namer.'';
 
 
 
echo' Here is your beautiful image on our server <img src="uploads/thumbs/'.$imagefile_name.'"/>';
 
 
ftp_copy("uploads/thumbs/".$imagefile_name."", "/wwwroot/media/".$imagefile_name.""); 
 
 
echo' Here is your beautiful image thumbnail <img src="http://www.***************.com/media/'.$imagefile_name.'"/>';
 
} 
else {
echo "Sorry, there was a problem uploading your file.";
}

Open in new window

Avatar of nanharbison
nanharbison
Flag of United States of America image

This may be a stupid question, but it is better to check this first:
On the form where you allow the file upload, are you setting the type -
<form enctype="multipart/form-data" ?

Also, you should be  setting a max file size checking and then checking the size of the file to see if it is too large,
And how are you setting the permissions on the file? I don't see that in your script.
Avatar of tepee
tepee

ASKER

Hi nanharbison, thanks for your reply. Yes the enctype has already been set.

That is the complete form just incase I have missed something. As I mentioned in the initial post the file upload works fine and the thumbnail scripts works well. The only problem exists around the ftp_put() command, and I can't figure it out.

Thanks
<form method="POST" enctype="multipart/form-data" action="process1.php">
	File to upload: <input type="file" name="imagefile"><br/>
	
	<input type="hidden" id="projectid" name="projectid" value="<?php echo $projectid; ?>"/>
	<input type="hidden" id="recordid" name="recordid" value="<?php echo $recordid; ?>"/>
	<input type="hidden" id="table" name="table" value="<?php echo $table; ?>"/>
	<input type="hidden" id="destination" name="desintation" value="<?php echo $destination; ?>"/>
	
	<input type="submit" value="Press"> to upload the file!
	</form>    

Open in new window

Avatar of tepee

ASKER

nanharbison - that's it... i think, When you mentioned in your second post I realized that I have set the permissions for the folder that I need to write the file to but have completely forgot the actually file needs written to itself. I will check this now and let you know.

Thanks
Avatar of tepee

ASKER

Hmmm, okay I don't know what I was thinking in that last post. On recap I am thinking that as long as the directory is set to CHMOD 777 I should be able to upload a file there no problem.

There are a few other things I could try going down the route of creating a temp file on the server, CHMOD to 777 and then use the fopen command to open the file and then try to copy the content of the file from the original to the new.

 I don't really want to code that if there isn't a need for it.

Any other suggestions on where the problem could be?

Thanks
Did you try setting the permission though? Most books I have read suggest setting permissions on uploaded files as they are uploaded. I can't find anything about CHMOD on folders and the files within them, but my experience has been that if I upload a file into a folder after the permission is set on a folder, that permission doesn't necessarily apply to the file.

Also, have you done a var_dump of the $_FILES array to see what you have there?
Avatar of tepee

ASKER

I tried setting the permissions of the uploaded thumbnail before the ftp function is called but it didn't make any difference. I am not sure if I need to change the permissions during the ftp_put thought. Should the permissions remain the same through that process?

I have attached the dump of the $_FILES Var below.

Thanks
array(1) { ["imagefile"]=> array(5) { ["name"]=> string(27) "2590393989_098f47c504_o.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(31) "C:\PHP5\upload_temp\php1E54.tmp" ["error"]=> int(0) ["size"]=> int(205611) } }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nanharbison
nanharbison
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tepee

ASKER

Brilliant, thats done the trick. Stupidity on my part for not paying more attention when slotting that in. Thanks for all of your help, its greatly appreciated.

Tim
Avatar of tepee

ASKER

Thanks again.
it took a while for me to notice that picky little detail!!!