views:

235

answers:

5

Hi, 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 couldnt 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 didnt.

any help would be appreciated.

A: 

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.

Sarfraz
i am trying to upload the same location as the php file resides. wwwroot folder. what do you mean by right upload settings. is there such setting on IIS.
murphy
A: 

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";
}
Cryo
i tried it. but still nothing in files bigger than 2 3KB even a 10KB files cannot upload. it still timeouts. as i said earlier i tried a few files like 1 2 kb and it uploads. i am already trying txt files of variuos sizes but cant upload.
murphy
@murphy Even after adding the FTP timeout setting the script times out? You can try dropping it to 10 seconds.
Cryo
if i run my script from the server (like http://localhost/3.php) it works fine but timeouts when trying from a client (http://10.47.47.1/3.php from client 10.47.47.13)i think it might be folder permissions or restrictions blocking the upload process. i didnt change the upload_tmp folder in php.ini so it is windows/temp.
murphy
@murphy Very strange. If that's the case it sounds like the problem is actually in the upload process itself, something is causing your web server to take a really long time processing that file. Do you by chance have any anti-virus software configured on your web server? If you increase the max_execution_time setting in your php.ini does your script eventually return when run from a remote host?
Cryo
i tried 10 seconds. but i dont think it works it still tries for like a few minutes. i also tried move_uploaded_file command instead of ftp_put by replacing all the ftp codes with if(move_uploaded_file($source_file, "$destination_file")) echo "ok"; else echo "nok";but it doesnt work :(
murphy
max_execution_time was by default 60 i tried 300 now returned to 60 :) nothing. anyway there is something conflicting but i couldnt sort it out. i will try to find out another way to upload my files. thanx
murphy
@murphy Try this: http://php.net/manual/en/features.file-upload.php#76587
Cryo
@murphy Are any errors being reported in your IIS error log?
Cryo
A: 

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?

Wilhelm Björklund
A: 

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

Alex
A: 

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.

qmega