Page 1 of 1

FTP Upload fails for larger files

Posted: Mon Oct 02, 2006 5:57 pm
by quackor
Hi, I am somewhat experienced with PHP, but this is my first approach to an FTP upload script, and I went through many tutorials to get this point. The way it acts is pretty puzzling, but I'll try to summarize my observations for your convinience.

I am not sure about exact borders between these - the server is slow and testing got painfully long.

1) It uploads a single file up to 1.2MB fine

2) With a 3.81 MB ftp_put fails and I get my own error message.

3) With a 9MB single file if breakes Firefox (browser updated today) displaying a page entitled "Problem loading page" and a message starting with "The connection to the server was reset while the page was loading." and some useless pointers on how to fix the problem.

4) It uploads five tiny files fine.

5) I tried three files around 150 KB each and it uploaded fine.

6) It failed same as in 3) when I tried four files 150 KB - 200KB each.

I sure as heck don't really know where to even start fixing this... so anyone who could point me in the right direction will gain my eternal gratitude and a thankyou in the comments of my super FTP upload once it works :D

Here's some code for you guys to laugh at.

Code: Select all

function putFiles ($login,$password) {
			$fileNames = array($_FILES["file1"]["name"], $_FILES["file2"]["name"], $_FILES["file3"]["name"], $_FILES["file4"]["name"], $_FILES["file5"]["name"]);
			$tempNames = array($_FILES["file1"]["tmp_name"], $_FILES["file2"]["tmp_name"], $_FILES["file3"]["tmp_name"], $_FILES["file4"]["tmp_name"], $_FILES["file5"]["tmp_name"]);
			$fileSizes = array();
			$fileExtensions = array();
			//Initialize sizes of the files
			for ($i=0; $i<5; $i++) { //
				$fileSizes[$i] = filesize($tempNames[$i]);
			}
			//Initialize array of file extensions
			for ($i=0; $i<5; $i++) {
				$ext = explode(".", $fileNames[$i]);
				$fileExtensions[$i] = $ext[count($ext) - 1];
			}
			
			$MAX_FILE_SIZE = 100000 * 1024; // No files larger than 100 MB
			
			$backstr = "<br><a href=\"index.php\">BACK</a>";
			
			// An array with file extensions that are prohibited
			$allExt = array("exe", "bat");
			
			//Check if no files exceed the sizelimit
			for ($i=0; $i<5; $i++) {
				if ($fileSizes[$i] > $MAX_FILE_SIZE) {
					die("File . $i .'s size exceeds $MAX_FILE_SIZE bytes.$backstr <br/>");
				}
			}
			// Go thru all the files submitted
			for ($i=0; $i<5; $i++) {
				//Connect to FTP
				$ftp_id = ftp_connect("ftp.totlpg.com");
				$ftp_login = ftp_login($ftp_id,$login,$password);
				if (!$ftp_login) {
					ftp_quit($ftp_id);
					die("Connection failed, check your login and password.$backstr <br/>");
				}
				//If file was submitted
				if (!empty($fileNames[$i])) {
					//Check is extension is not prohibited
					if (!in_array(strtolower($fileExtensions[$i]), $allExt)) { 
						if (ftp_put($ftp_id,$fileNames[$i],$tempNames[$i],FTP_BINARY)) {
							echo ("Uploading " . $fileNames[$i] . " was <FONT COLOR=\"red\">successful</font><br/>.");
							ftp_quit($ftp_id);
						}	
						//Check if upload succeded
						else {
							ftp_quit($ftp_id);
							echo("<FONT COLOR=\"RED\">Upload failed for file " . ($i+1) . ". It is possible that a file with that name already exists on our server, please click BACK, change the name and try again.</font>"  . $backstr ."<br/>");
						}
					}
					else {
						ftp_quit($ftp_id);
						die ("ERROR - File " . ($i+1) . "'s extension is not allowed!" . $backstr . "<br/>");
					}
				}
			}
		}

Posted: Mon Oct 02, 2006 6:25 pm
by Christopher
It sounds like either the maximum file size or the maximum script execution time is being exceeded -- or both. Do you have control over those settings?

Posted: Mon Oct 02, 2006 6:33 pm
by quackor
Well if those are somewhere in the PHP config on our server than not directly... but I can always call "the guy" and bug him about those settings you mentioned. Any other ideas what it could be?

Posted: Mon Oct 02, 2006 7:02 pm
by Christopher
You can sometimes set them in the script with set_ini() or in a .htaccess file. Check the manual and do a phpinfo() to see what your settings are. It sounds like the 3.81 MB file is bigger than the max size, and the 9MB is taking too long and causing a timeout.