奇怪的PHP文件上传问题

I am having strange issues regarding file upload on my windows system. I am using windows 7 with iis7 on the server. I am trying on a client comp with local IP 10.47.47.13 and the server is 10.47.47.1.

I have a very simple form which i couldn't make it work in some cases. The page stays on the wwwroot. (http://10.47.47.1/3.php)

3.php

<?php
 $source_file=$_FILES["newsimg"]["tmp_name"];   
 $destination_file="123.jpg";
 $ftp_server="localhost";
 $ftp_username="admin";
 $ftp_password="apple";

  if ($source_file!="") {
    $mrph_connect = ftp_connect($ftp_server,21);
    $mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);   
    if (($mrph_connect) && ($mrph_login)) {
      $upload = ftp_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
      if ($upload) echo "ok"; else echo "nok";
    }
  }

?>

<body>
<form enctype="multipart/form-data" action="3.php" method="POST">
  <input type=file  name=newsimg>
  <input type=submit name=mrph>
</form>
</body>

The form calls itself to upload the file. When I select a file of size 1 or 2 KB it works but when I select a file of even 10 15KB the page timeouts after some time. I checked the php.ini settings file upload is on, I set temp folder as c:\uploads just to test. AS I SAID IT WORKS FOR FILES SIZE 1 OR 2KB BUT NOT EVEN WHEN I SELECT A FILE OF 10 OR 20KB. I even removed the PHP code (commented everything) to see even when nothing is done it works but it didn't.

Any help would be appreciated.

To me, the problem seems to be where you are uploading your file, the server; there is nothing wrong with uploading because you are able to upload smaller files but when you upload files of 20 kb size, you fail, check to make sure that right upload settings are specified on the server you want to upload the file to. Using ftp and uploading to a different server/location itself is slow process though. Your code also seems to be right.

My guess is that your ftp_put is timing out, try setting your FTP timeout threshold below PHP's default (30 seconds):

$mrph_connect = ftp_connect($ftp_server,21);

ftp_set_option($mrph_connect, FTP_TIMEOUT_SEC, 20);

$mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);   
if (($mrph_connect) && ($mrph_login)) {
  $upload = ftp_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
  if ($upload) echo "ok"; else echo "nok";
}

If making that adjustment causes your script to return 'nok' then you'll know the put is taking too long.

If the put is your problem you try a non-blocking put with ftp_nb_put to FTP the file asynchronously:

$mrph_connect = ftp_connect($ftp_server,21);
$mrph_login= ftp_login($mrph_connect, $ftp_username, $ftp_password);   
if (($mrph_connect) && ($mrph_login)) {

  $ret = ftp_nb_put($mrph_connect, $destination_file, $source_file, FTP_BINARY);
  while ($ret == FTP_MOREDATA) {
    $ret = ftp_nb_continue($mrph_connect);
  }

  if ($ret == FTP_FINISHED) echo "ok"; else echo "nok";
}

I think Cryo is onto something, can it be that the php.ini file isn´t correctly configured and the maximum filesize is to low?

This might not be it but for the record your form should have a MAX_FILE_SIZE hidden input with the number of bytes corresponding to the max upload size

You might have a low filesize limit. To check this: create a new php file, called info.php or whatever and just write

<?php
phpinfo();
?>

Open that page in your browser, and search for upload_max_filesize. Check the value for that; if it is only a few kilobytes, that's your problem. If this is the case, you will have to modify your php.ini (under Apache you could use a directive in a .htaccess file as well, but I don't think there's anything like that for IIS). The location of this file can be different depending on your installation, but it's probably C:\Windows\php.ini. Find the upload_max_filesize directive and change it to something bigger. The default is 2 megabytes (2M) but you can make it whatever.