Home > Archive > PHP Language > May 2004 > fput writes 0kb file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
fput writes 0kb file
|
|
|
| Hi
I'm having a lot of trouble with this and I hope someone
can help. I'm writing a script that is installed on many different
servers, so compatibility is obviously an issue.
This code works fine on my server, which runs
PHP Version 4.3.3
System Linux 2.4.20-20.7 #1 Mon Aug 18 14:56:30 EDT 2003 i686
But it doesn't work on this server, (and many others) which runs
PHP Version 4.3.3
System Linux 2.4.25-1-grsec #1 SMP Tue Feb 24 12:05:40 CST 2004 i686
PROBLEM:
The code runs completely fine, no errors, except I get a 0kb file.
Here is the code:
$fp = ftp_connect("$ftphost"); //$ftphost = www.somedomain.com
if ($fp)
{
$login = ftp_login ($fp, $user, $pass);
if ($directory)
ftp_mkdir($fp, "$basedir$directory");
@ftp_delete ($fp, $ftpfile); //deletes the file if there, if not, silence
warning
ftp_quit($fp);
}
else die('Cannot connect to FTP');
$fp = fopen("ftp://$user:$pass@$ftphost$ftpfile", 'w'); //user and pass
are the correct values
if ($fp)
{
//all these variables are as they should be, they get printed
echo "<b>debugging</b> <br>";
echo "<textarea>$contents</textarea> <br>"; //is NOT empty
echo "ftpfile: $ftpfile <br>"; // ftpfile= /www/test6.html
fputs($fp, $contents) or die ("can't fput");
fclose($fp);
}
else die('Cannot create file via FTP');
Like I said, the file gets created, but with 0kb. I tried fwrite, I tried
fputs($fp, $contents, strlen($contents));
I have no idea why this works for only 50% of the servers I try it on.
Is is a permissions issue, does it depend on what version of linux they're
running?
etc.
I searched the net, I googled all the php groups, and found nothing that
helped.
If you have any idea at all, it would be greatly appreciated.
Thanks
| |
|
| On Tue, 25 May 2004 14:51:26 -0400, Jonah <nomail@nomail.com> wrote:
> Hi
>
> I'm having a lot of trouble with this and I hope someone
> can help. I'm writing a script that is installed on many different
> servers, so compatibility is obviously an issue.
>
> This code works fine on my server, which runs
> PHP Version 4.3.3
> System Linux 2.4.20-20.7 #1 Mon Aug 18 14:56:30 EDT 2003 i686
>
> But it doesn't work on this server, (and many others) which runs
> PHP Version 4.3.3
> System Linux 2.4.25-1-grsec #1 SMP Tue Feb 24 12:05:40 CST 2004 i686
>
> PROBLEM:
> The code runs completely fine, no errors, except I get a 0kb file.
>
% SNIP %
>
> I have no idea why this works for only 50% of the servers I try it on.
> Is is a permissions issue, does it depend on what version of linux
> they're
> running?
> etc.
> I searched the net, I googled all the php groups, and found nothing that
> helped.
> If you have any idea at all, it would be greatly appreciated.
> Thanks
>
>
>
>
Hey,
I ran into the same problems when dealing with something similar, but my
problems were even more intermittent, it would sometimes upload the file
fine, and sometimes just a 0kb version (to the same server!) I couldn't
narrow it down, so out of desperation I went back to using the very basic
ftp commands rather then the new easier ftp supporting versions
(fopen/fputs etc) which meant I had to write a temporary file to the
localserver before using ftp_connect/ftp_login/ftp_put, once I started
using them, I had no problems at all. Maybe worth a shot for you, I've
included the code for the main upload function beneath, but it's generally
just standard stuff. Just stick to the ftp_* commands :]
Hope that helps, I just assumed it was a buggy implementation of the new
stream handlers, but didn't have time to track it down to something
precise and report it.
Dank.
# FUNCTION 1 : FTP 'UPLOAD' SCRIPT
# INPUTS:
# Connection Data Array (username,password,ftpaddress,port,subdi
r)
# File For Upload Array (dirpath, filename)
#
# Example Usage:
# include 'dbconnect.php';
# $localfile = array('testfile.txt','/home/user/public_html/subdir/');
# $moo = upload($localfile, 1);
# echo ($moo ? "YAY" : "NAY");
FUNCTION upload($fileinfo, $id)
{
$connection = upload_array($id);
#upload_array($id) fills in the connection data array for relevant id(see
INPUTS)
$connect = ftp_connect($connection[2]);
$login = ftp_login($connect, $connection[0], $connection[1]);
## Check connection ##
if ((!$connect) || (!$login))
{
$error = 1;
return 1;
exit;
}
else
{
$error = 0;
}
## UPLOAD FILE ##
ftp_chdir($connect, $connection[4]);
$upload = ftp_put($connect, $fileinfo[0], $fileinfo[1].$fileinfo[0],
FTP_BINARY);
## CHECK ##
if (!$upload)
{
$error = 1;
}
else
{
$error = 0;
}
ftp_close($connect);
return($error);
}
--
If we can't play God, who will?
| |
|
| >
> Hey,
>
> I ran into the same problems when dealing with something similar, but my
> problems were even more intermittent, it would sometimes upload the file
> fine, and sometimes just a 0kb version (to the same server!) I couldn't
> narrow it down, so out of desperation I went back to using the very basic
> ftp commands rather then the new easier ftp supporting versions
> (fopen/fputs etc) which meant I had to write a temporary file to the
> localserver before using ftp_connect/ftp_login/ftp_put, once I started
> using them, I had no problems at all.
Hey, thanks for the help, you're a life saver.
I did as you suggested, however, I can't seem to get ftp_put to work.
I'm actually as to how it works.
Here's my code
//CREATE TEMPORARY FILE IN SAME DIRECTORY AS SCRIPT
$tempfile = $filename.".html";
$destination_path = "/home/user/public_html/";
$tfp = fopen($tempfile, "w") or die ("Can't create $tempfile on
".__LINE__);
fwrite($tfp, $contents) or die ("Can't write to $tempfile on ".__LINE__);
fclose($tfp) or die ("Can't close $tempfile on ".__LINE__);
//----------file is created succesfully
//UPLOAD FILE ON SAME SERVER TO ANOTHER DIRECTORY
$ftp_server = "domain.com"; //does this always work without putting
ftp.domain.com? I wonder
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
$ftp_server on ".__LINE__);
ftp_login($conn_id, $user, $pass) or die ("Couldn't login to $ftp_server
using $user, $pass on ".__LINE__);
ftp_chdir($conn_id, $basedir) or die ("Couldn't change directory to
$basedir on ".__LINE__);
ftp_put($conn_id, $tempfile, "/home/user/public_html/".$tempfile,
FTP_ASCII) or die ("Couldn't ftp_put $tempfile on ".__LINE__);
ftp_close($conn_id) or die ("Couldn't ftp_close on ".__LINE__);
I get an error at ftp_put -> "Couldn't ftp_put $tempfile .. "
Maybe there is another way of solving this problem?
Ultimately, what I want to do is create an html file anywhere on the users's
server.
I can't use fopen because of permission issues.
Well, I can, if the user chmods his public_html directory to 777, but I'm
not sure if that's safe,
and even then, i don't think it will always work.
So I'm using your solution above.
Is there another way to do this? Anyone? I've been working for days on this
with no
solution.
| |
| Shane Lahey 2004-05-30, 12:31 am |
| On Tue, 25 May 2004 14:51:26 -0400, "Jonah" <nomail@nomail.com> wrote:
looks fine, make sure socket is really open with is_resource() & don't
enclose $ftphost in ""
edited source below
<?php
$fp = ftp_connect($ftphost);
if (is_resource($fp))
{
$login = ftp_login ($fp, $user, $pass);
if ($directory)
ftp_mkdir($fp, "$basedir$directory");
@ftp_delete ($fp, $ftpfile); //deletes the file if there, if
not, silence warning
ftp_quit($fp);
}
else
die('Cannot connect to FTP');
$fp = fopen("ftp://$user:$pass@$ftphost$ftpfile", 'w'); //user and
pass are the correct values
if (is_resource($fp))
{
//all these variables are as they should be, they get printed
echo "<b>debugging</b> <br>";
echo "<textarea>$contents</textarea> <br>"; //is NOT empty
echo "ftpfile: $ftpfile <br>"; // ftpfile= /www/test6.html
fputs($fp, $contents) or die ("can't fput");
fclose($fp);
}
else
die('Cannot create file via FTP');
?>
>Hi
>
>I'm having a lot of trouble with this and I hope someone
>can help. I'm writing a script that is installed on many different
>servers, so compatibility is obviously an issue.
>
>This code works fine on my server, which runs
>PHP Version 4.3.3
>System Linux 2.4.20-20.7 #1 Mon Aug 18 14:56:30 EDT 2003 i686
>
>But it doesn't work on this server, (and many others) which runs
>PHP Version 4.3.3
>System Linux 2.4.25-1-grsec #1 SMP Tue Feb 24 12:05:40 CST 2004 i686
>
>PROBLEM:
>The code runs completely fine, no errors, except I get a 0kb file.
>
>Here is the code:
> $fp = ftp_connect("$ftphost"); //$ftphost = www.somedomain.com
> if ($fp)
> {
> $login = ftp_login ($fp, $user, $pass);
> if ($directory)
> ftp_mkdir($fp, "$basedir$directory");
> @ftp_delete ($fp, $ftpfile); //deletes the file if there, if not, silence
>warning
> ftp_quit($fp);
> }
> else die('Cannot connect to FTP');
>
> $fp = fopen("ftp://$user:$pass@$ftphost$ftpfile", 'w'); //user and pass
>are the correct values
> if ($fp)
> {
>//all these variables are as they should be, they get printed
> echo "<b>debugging</b> <br>";
> echo "<textarea>$contents</textarea> <br>"; //is NOT empty
> echo "ftpfile: $ftpfile <br>"; // ftpfile= /www/test6.html
>
> fputs($fp, $contents) or die ("can't fput");
> fclose($fp);
> }
> else die('Cannot create file via FTP');
>
>Like I said, the file gets created, but with 0kb. I tried fwrite, I tried
> fputs($fp, $contents, strlen($contents));
>
>I have no idea why this works for only 50% of the servers I try it on.
>Is is a permissions issue, does it depend on what version of linux they're
>running?
>etc.
>I searched the net, I googled all the php groups, and found nothing that
>helped.
>If you have any idea at all, it would be greatly appreciated.
>Thanks
>
>
>
|
|
|
|
|