Solved

ftp_put problem

Posted on 2009-06-28
11
1,625 Views
Last Modified: 2013-11-10
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

0
Comment
Question by:tepee
  • 6
  • 5
11 Comments
 
LVL 17

Expert Comment

by:nanharbison
ID: 24731035
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,
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 24731049
And how are you setting the permissions on the file? I don't see that in your script.
0
 

Author Comment

by:tepee
ID: 24731051
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

0
 

Author Comment

by:tepee
ID: 24731059
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
0
 

Author Comment

by:tepee
ID: 24731161
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Expert Comment

by:nanharbison
ID: 24731221
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?
0
 

Author Comment

by:tepee
ID: 24731260
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

0
 
LVL 17

Accepted Solution

by:
nanharbison earned 125 total points
ID: 24731338
so it getting everything.
How about moving

ftp_pasv($conn_id,true);

underneath the $login_result and not in the if (there is no connection or login) statement
0
 

Author Comment

by:tepee
ID: 24731356
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
0
 

Author Closing Comment

by:tepee
ID: 31597669
Thanks again.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 24731362
it took a while for me to notice that picky little detail!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now