uploading files using FTP. help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invertigo
Forum Newbie
Posts: 3
Joined: Sat Oct 25, 2003 2:22 pm

uploading files using FTP. help!

Post by invertigo »

Hello,

I am currently trying to get file uploading working, using an FTP connection via fopen/fwrite. The files are created in the correct folder on the server, but they are always at 0 length. The web server is Apache, and my host says that it should work using the commands specified. I have been successful using move_uploaded_file.
Can anyone please offer some advice? Code and debugged output are as follows:

Code: Select all

function upload() {


	$img_array=array("image/jpeg","image/pjpeg","image/gif","image/png","image/x-png");
	for($i=0;$i<count($_FILES['image']['tmp_name']);$i++) {

	
		echo "loop entered ".$i."<br> \n\n";
		echo $_FILES['image']['tmp_name'][$i]."<br>\n";				//debug

		$filesize=$_FILES['image']['size'][$i];     // filesize

		$filetype=$_FILES['image']['type'][$i];     // mime type

		$filename=$_FILES['image']['name'][$i];     // original name

		$filetemp=$_FILES['image']['tmp_name'][$i]; // temporary name

	

		$file=explode(".", $filename);

		$file[0] .= date("U");
		$file[0] .= $i;

		$filename=$file[0].".".$file[1];              //add unix time and loop counter 
															//to filename for uniqueness

	
	$uploaddir='/home/dkhdes/public_html/images/';
	
		switch ($i) {

		case 0:

			$imaget=$filename;

			echo "switch entered ".$i."<br>\n";							//debug
			echo "thumb image name is: ".$imaget."<br> \n";		//debug
			break;

		case 1:

			$image1=$filename;

			echo "switch entered ".$i."<br>\n";							//debug
			echo "image1 name is: ".$image1."<br> \n";		//debug
			break;

		case 2:

			$image2=$filename;
			echo "switch entered ".$i."<br>\n";							//debug
			echo "image2 name is: ".$image2."<br> \n";		//debug
			break;

			}

	


		if (($filesize>0)||($filesize<2000000))    {

			if (in_array($filetype,$img_array))

			{
	
		
				if ($thePic = fopen ($filetemp, "rb"))

				{

																	//read temp file into var

					$savePic = fread($thePic, $filesize);

	

																//Open a new file in the 
																//correct directory using the 
																//name of the uploaded pic file


	
				

					$fileHandle = fopen("ftp://$name:$password@ftp.dkhdesign.net/public_html/images/$filename", "wb");	
					if ($fileHandle)

						{

																								  //Put data into just opened file pointer
							if (fwrite ($fileHandle, $savePic, strlen($savePic)))

							{

								echo "file string length is: ".strlen($savePic)."<br>\n";		//debug
								 echo "file <b>$filename</b> successfully written <br><br>\n";

							}

							else

							{

								  echo "file <b>$filename</b> was NOT written<br><br>\n";

							 }

							fclose ($fileHandle);
						}

				

					fclose ($thePic);

					}

					else
									 // The file was not created.  Inform the user.
					{

						echo "<b> The file: $filetemp could not be created!</b><br>\n";

					}

				
				




		}

	}
	}				
	return array($imaget,$image1,$image2);		//returns the filenames of uploaded 
files
}



list ($imaget,$image1,$image2)=upload();
The output in the browser, including the debug messages, is as follows:

loop entered 0
/tmp/phpRh1uqv
switch entered 0
thumb image name is: spacer10670042670.gif
GIF89a€!ù,D;file string length is: 43
file spacer10670042670.gif successfully written

loop entered 1
/tmp/phpDRX5Bk
switch entered 1
image1 name is: spacer10670042671.gif
GIF89a€!ù,D;file string length is: 43
file spacer10670042671.gif successfully written

loop entered 2
/tmp/phpRx83M9
switch entered 2
image2 name is: spacer10670042682.gif
GIF89a€!ù,D;file string length is: 43
file spacer10670042682.gif successfully written
:?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what happens if you use

Code: Select all

if (($written=fwrite ($fileHandle, $savePic, strlen($savePic))) !== false )
{
	echo "file string length is: ".strlen($savePic)."<br>\n";      //debug
	if( $written!=strlen($savePic) )
		echo 'not all of the content has been written<br />';
	else
		echo "file <b>$filename</b> successfully written <br><br>\n";
}
?

Do you delete the files on the ftp-server between two attempts or are the filenames unique for each attempt? (the ftp url_wrapper is only able to write new files)
invertigo
Forum Newbie
Posts: 3
Joined: Sat Oct 25, 2003 2:22 pm

Post by invertigo »

thanks volka. I tried your suggestion, but still no success.
The file name writes to the server, but just has a zero file length. The file names are unique for each attempt, as I add a suffix to each attempted file name using the date() function. <sigh>
invertigo
Forum Newbie
Posts: 3
Joined: Sat Oct 25, 2003 2:22 pm

Post by invertigo »

Oh, for there to be an answer to my problem! :roll:
Post Reply