We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


ftp_put problem

tepee asked
Medium Priority
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

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
function createthumb($name,$filename,$new_w,$new_h){ 
   if (preg_match('/\.(jpg|jpeg)$/',$name)){ 
   } else if (preg_match('/\.png$/',$name)){ 
   } else { 
     die("Issue with thumbnail creation around function call preg match, make sure you didn't upload a .gif file"); 
if ($old_x > $old_y) {
if ($old_x < $old_y) {
if ($old_x == $old_y) {
	if (preg_match("/png/",$system[1]))
} else {
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";
	$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
	if ($upload === false) {  // check upload status
         echo "<h2>FTP upload  has failed!</h2> <br />";
      } else {
         echo "Uploading Complete!<br /><br />";
<div id="wrapper">
<?php  include ('includetop.php');  ?>
<div id="loadwindow">
  $target = "uploads/"; 
$target = $target . basename( $_FILES['imagefile']['name']) ; 
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 */
$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

Watch Question

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.


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.

<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!

Open in new window


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.



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?

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?


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.

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

Unlock this solution and get a sample of our free trial.
(No credit card required)


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.



Thanks again.
it took a while for me to notice that picky little detail!!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.