uploading files using FTP. help!
Posted: Sat Oct 25, 2003 2:22 pm
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:
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

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();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