Uploading a local file to site FTP server using PHP
Moderator: General Moderators
Dear All - I've more or less reached a dead end with this one. I'm not incredibly proficient at uploading/downloading, as this is the first time I have done it, so if this problem is obvious I probably still don't see it.
I'm confused with how uploading takes place, and what function - either ftp_put or ftp_get - I should be using if I want to transfer a file from my computer to my web site's server, with the PHP code being executed on my web site's server.
Is anyone able to spot the issues?
Many thanks
Mark
I'm confused with how uploading takes place, and what function - either ftp_put or ftp_get - I should be using if I want to transfer a file from my computer to my web site's server, with the PHP code being executed on my web site's server.
Is anyone able to spot the issues?
Many thanks
Mark
Sorry to drag this on but I really need to sort this out! Here's the situation clearly in words.
I have a file, MyFile.doc on the D:\ drive of my computer.
I have my site, hosted on a remote server. Every time I write a PHP script, because I don't have PHP installed on my computer, I upload my scripts to my server and run them from the server.
I simply want to copy MyFile.doc, stored on the D:\ drive of my computer, to the remote server on which my site is stored, using PHP code executed on the remote server.
Can anyone please explain how this is done, I would be very grateful.
I have a file, MyFile.doc on the D:\ drive of my computer.
I have my site, hosted on a remote server. Every time I write a PHP script, because I don't have PHP installed on my computer, I upload my scripts to my server and run them from the server.
I simply want to copy MyFile.doc, stored on the D:\ drive of my computer, to the remote server on which my site is stored, using PHP code executed on the remote server.
Can anyone please explain how this is done, I would be very grateful.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
I think you have permissions porblems on you server the error is that it can't open the new file on the server for writing, right? check the permissions where you are writing to! I think you already have it working, but it just can't write to the server because its not allowed to.
Last edited by magicrobotmonkey on Tue Mar 23, 2004 7:16 am, edited 1 time in total.
have a look at this post, it answers your question pretty simply, you need two parts, you need the html form and the php script to process the file after the form is processed. Where the file is on your local machine is irrelevant as the html form will deal with all of that.
http://www.devnetwork.net/forums/viewtopic.php?t=19787
http://www.devnetwork.net/forums/viewtopic.php?t=19787
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
Mark did you fix what I asked you to fix eariler? That is most likely your problem. I don't think it's a permission error because it's just going to the temp php upload folder. I still think your form is wrong. Post your form code too. If it doesn't match my example on the previous page, the interface between then form and the processing then it is not going to work.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Ok, I think your code was working here:
Because look at your error; its an error opening on the server, not on the local machine. I think if you use the above code, which generated this error, and check the permissions on your server, you will be all set.
Code: Select all
<?php
$conn = ftp_connect("$host");
if(!$conn)
{
echo 'Error: could not connect to FTP server. Please try again later.';
exit();
}
echo "Connect to $host.<br />";
// log in to host
@ $result = ftp_login($conn, $user, $pass);
if(!$result)
{
echo "Error: Could not log on as $user<br />";
ftp_quit($conn);
exit();
}
echo "Logged in as $user.";
echo "Uploading file:";
$filename = $_POST['photo1_upload'];
echo $filename;
if(!$success = ftp_get($conn, '/tmp/test.mp3', $filename, FTP_BINARY))
{
echo 'Error: Could not upload file to server';
ftp_quit($conn);
exit();
}
echo 'File downloaded successfully';
ftp_quit($conn); ?>Because look at your error; its an error opening on the server, not on the local machine. I think if you use the above code, which generated this error, and check the permissions on your server, you will be all set.
also, i just noticed, not sure if this will make a difference, but you may have the wrong slashes in your server save pathConnect to ftp.XXXX.biz.
Logged in as XXXX.Uploading file:D:\\BreakXL7.mp3
Warning: ftp_get(): Error opening /tmp/test.mp3 in \\XXXX\domains\s\XXXX.biz\user\htdocs\XXXX.php on line 2405
Error: Could not upload file to server
This is the form, summarised:
As you can see, because I have more than one URL to which I may need to submit POST data depending upon which button the user clicks, I have used JavaScript to post the data to these different URLs. Sorry this whole thing is getting rather complicated but it is a complicated section of the site.
Thanks Mark
Code: Select all
<FORM METHOD="post" NAME="myForm" ENCTYPE="multipart/form-data">
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="718" STYLE="padding-left:16px;padding-right:16px" BGCOLOR="#000066"><FONT COLOR="#FFFFFF" FACE="Tahoma" SIZE="2">
</FONT></TD>
</TR>
<TR>
<TD HEIGHT="10" WIDTH="718" STYLE="padding-left:16px;padding-right:16px" BGCOLOR="#000066"><FONT COLOR="#FFFFFF" FACE="Tahoma" SIZE="2">
</FONT></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
ID Number
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<INPUT TYPE="text" LENGTH="100" SIZE="15" NAME="txt_ID"> e.g. DE-10-157
</FONT></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Title
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSIONї'property_ID'])) echo '<INPUT TYPE="text" LENGTH="100" SIZE="26" NAME="txt_PropTitle" VALUE="'.$_SESSIONї'ID'].'">';
else echo '<INPUT TYPE="text" LENGTH="100" SIZE="26" NAME="txt_Title">';
?>
</FONT></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Item 1
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSIONї'itemone'])) echo '<INPUT TYPE="text" LENGTH="5" SIZE="3" NAME="txt_ItemOne" VALUE="'.$_SESSIONї'bedrooms'].'">';
else echo '<INPUT TYPE="text" LENGTH="5" SIZE="3" NAME="txt_Bedrooms">';
echo ' e.g. 3';
?>
</FONT></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Short Description
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSIONї'smalldesc'])) echo '<TEXTAREA NAME="txt_SmallDesc", ROWS=2, COLS=50>'.$_SESSIONї'smalldesc'].'</TEXTAREA>';
else echo '<TEXTAREA NAME="txt_SmallDesc", ROWS=2, COLS=50></TEXTAREA>';
echo ' <BR>Recommended no more than 20 words';
?>
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Long Description
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSIONї'bigdesc'])) echo '<TEXTAREA NAME="txt_BigDesc", ROWS=10, COLS=70>'.$_SESSIONї'bigdesc'].'</TEXTAREA>';
else echo '<TEXTAREA NAME="txt_BigDesc", ROWS=10, COLS=70></TEXTAREA>';
echo ' <BR>Recommended no more than 100 words';
?>
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<STRONG>Price</STRONG>
</FONT></TD>
<TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
<?php
if(isset($_SESSIONї'price'])) echo '<INPUT TYPE="text" LENGTH="100" SIZE="15" NAME="txt_Price">';
echo '<INPUT TYPE="text" LENGTH="100" SIZE="15" NAME="txt_Price">';
echo ' Euros';
?>
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<?php
do_html_title('<STRONG>Upload Photographs to Database</STRONG>');
?>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
</TD>
</TR>
<TR>
<TD HEIGHT="20" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Note that Photo 1 will appear as the main, largest photograph on the property information page. Click on preview to view the arrangement of photographs on the page.
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066">
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="122" WIDTH="10" BGCOLOR="#000066">
</TD>
<TD HEIGHT="122" WIDTH="175" BGCOLOR="#000000" ALIGN="CENTER" VALIGN="CENTER"><FONT SIZE="1" COLOR="#FFFFFF" FACE="Tahoma">
Photo 1
</FONT></TD>
<TD HEIGHT="122" WIDTH="10" BGCOLOR="#000066">
</TD>
<TD HEIGHT="122" WIDTH="175" BGCOLOR="#000000" ALIGN="CENTER" VALIGN="CENTER"><FONT SIZE="1" COLOR="#FFFFFF" FACE="Tahoma">
Photo 2
</FONT></TD>
<TD HEIGHT="122" WIDTH="10" BGCOLOR="#000066">
</TD>
<TD HEIGHT="122" WIDTH="175" BGCOLOR="#000000" ALIGN="CENTER" VALIGN="CENTER"><FONT SIZE="1" COLOR="#FFFFFF" FACE="Tahoma">
Photo 3
</FONT></TD>
<TD HEIGHT="122" WIDTH="10" BGCOLOR="#000066">
</TD>
<TD HEIGHT="122" WIDTH="175" BGCOLOR="#000000" ALIGN="CENTER" VALIGN="CENTER"><FONT SIZE="1" COLOR="#FFFFFF" FACE="Tahoma">
Photo 4
</FONT></TD>
<TD HEIGHT="122" WIDTH="10" BGCOLOR="#000066">
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="5" WIDTH="750" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="718" STYLE="padding-left:16px;padding-right:16px" BGCOLOR="#000066">
<FONT FACE="Tahoma" COLOR="#FFFFFF" SIZE="2">
Select photo files (JPEG or GIF format only) for upload, then click on the 'Upload Photos' button below. No files will be uploaded until all photo files have been selected for upload.
</FONT>
</TD>
</TR>
<TR>
<TD HEIGHT="10" WIDTH="718" STYLE="padding-left:16px;padding-right:16px" BGCOLOR="#000066">
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="20" WIDTH="318" BGCOLOR="#000066" STYLE="padding-left:16px"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
Photo 1 <INPUT TYPE="file" NAME="photo1_upload"><BR>
Photo 2 <INPUT TYPE="file" NAME="photo2_upload"><BR>
Photo 3 <INPUT TYPE="file" NAME="photo3_upload"><BR>
Photo 4 <INPUT TYPE="file" NAME="photo4_upload"><BR>
</FONT></TD>
<TD HEIGHT="20" WIDTH="400" BGCOLOR="#000066" STYLE="padding-right:16px" VALIGN="CENTER" ALIGN="LEFT">
<INPUT TYPE="hidden" NAME="input1" VALUE=""/>
<A HREF="javascript: upload()"><IMG SRC="upload.jpg" STYLE="border:none"></A>
</TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="30" WIDTH="80" BGCOLOR="#000066" STYLE="padding-left:15px" ALIGN="CENTER"><A HREF="javascript: preview()"><IMG SRC="preview.jpg" STYLE="border:none"></A></TD>
<TD HEIGHT="30" WIDTH="515" BGCOLOR="#000066" STYLE="padding-left:15px"><A HREF="javascript: clear()"><FONT COLOR="#FFFFFF"><IMG SRC="clear.jpg" STYLE="border:none"></A></TD>
<TD HEIGHT="30" WIDTH="85" BGCOLOR="#000066" STYLE="padding-right:10px" ALIGN="CENTER"><A HREF="javascript: sub();"><IMG SRC="submit.jpg" STYLE="border:none"></A></TD>
</TR>
</TABLE>
<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
<TR>
<TD HEIGHT="5" WIDTH="750" BGCOLOR="#000066">
</TD>
</TR>
</TABLE>
</TABLE>
</FORM>
<SCRIPT language="JavaScript">
function upload()
{
document.getElementById('myForm').action = "run2.php?p=27";
document.getElementById('myForm').submit();
}
function preview()
{
document.getElementById('myForm').action = "run2.php?p=24";
document.getElementById('myForm').submit();
}
function clear()
{
document.getElementById('myForm').action = "run2.php?p=26";
document.getElementById('myForm').submit();
}
function sub()
{
document.getElementById('myForm').action = "run2.php?p=25";
document.getElementById('myForm').submit();
}
</SCRIPT>Thanks Mark
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA