Solved

ftp_put works with Text based files but not Images

Posted on 2007-04-03
3
268 Views
Last Modified: 2009-12-16
I'm using ftp_put to upload files.  It works with text based files but not on images. I've tried with small file sized images and used FTP_BINARY. The images are well under the upload liimit and ftp iIS enabled with PHP.

I'm using the following script to FTP files to server.

<?php
if($submit){

// set up basic connection
$ftp_server = "www.skunk2.com";
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "****";
$ftp_user_pass = "******";
//capture file source from form
$source_file = $file;
$destination_file = "test.jpg";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

ftp_chdir($conn_id, "websites");
ftp_chdir($conn_id, "www.skunk2.com");
ftp_chdir($conn_id, "html");

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

// 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);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

      <head>
            <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
            <meta name="generator" content="Adobe GoLive">
            <title>Untitled Page</title>
      </head>

      <body bgcolor="#ffffff">
            <form id="FormName" action="ftpTest.php" method="get" name="FormName">
                  <input type="file" name="file" size="41"><br>
                  <br>
                  <input type="submit" name="submit" value="submit">
            </form>
            <p></p>
      </body>

</html>
0
Comment
Question by:boynatural
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 125 total points
ID: 18850898

I don't see how this code works at all.

First, the form isn't setup correctly.  It shold be a POST not a GET.  Also try adding enctype to the form.

like this:

<form id="FormName" action="ftpTest.php" method="POST" name="FormName" enctype="multipart/form-data">


Second, you probably don't need the ftp_chdir lines.  Try removing them and see if your file gets uploaded to the default directory for the ftp user account that you are using. (change the form as specified above)

You probably want to name the file on your server the same as it was on the submitted from the form.

$destination_file = $_FILES['file']['name'];



0
 

Author Comment

by:boynatural
ID: 18853327
I was using the form to pass the location. By adding your adjustment it worked with images. Does the form actually pass the file or just a reference to location??
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 18854122

The form passes the filename parameter needed by the PHP process.  The PHP code is actually skipped based on the results of this test:  

if($submit)

If the form isn't submitted, then it just displays the form.

I'm not sure why it worked for you on text files.  I have no explanation on that.
0

Featured Post

What Is Threat Intelligence?

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

Join & Write a Comment

Suggested Solutions

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

15 Experts available now in Live!

Get 1:1 Help Now