Uploading a local file to site FTP server using PHP

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

Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

southeastweb he is currently wanting to upload an MP3, so i suppose thats what he wants to do.. use it to upload MP3's.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hello - no, I actually want to upload photos of items on the database. I'm uploading an MP3 as a test run.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

I think you guys are missing the point! A file is a file, and they all upload the same!

Check that the file has actually been transfered from your machine to the web server.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Wayne, as I said a bit earlier, the file itself does appear in /tmp/, but it is 0 bytes in size (i.e. nothing has been written, the file has only been registered), and the error messages I stated appear.

Any help would be much appreciated!

Many thanks

Mark
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Aaarrgghh! This is driving me insane!!
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

/bump
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

If I have the file D:\MyFile.doc, in what format do I express this pathname so that the upload functions can locate it?

E.g. something like D:\\MyFile.doc would work, or would it have to be file://XXXXX....

These are all small things that are very difficult to find out!

Thanks for your help

Mark
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

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

Post by magicrobotmonkey »

Ok, I think your code was working here:

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.
Connect 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
also, i just noticed, not sure if this will make a difference, but you may have the wrong slashes in your server save path
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

This is the form, summarised:

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&#1111;'property_ID'])) echo '<INPUT TYPE="text" LENGTH="100" SIZE="26" NAME="txt_PropTitle" VALUE="'.$_SESSION&#1111;'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&#1111;'itemone'])) echo '<INPUT TYPE="text" LENGTH="5" SIZE="3" NAME="txt_ItemOne" VALUE="'.$_SESSION&#1111;'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&#1111;'smalldesc'])) echo '<TEXTAREA NAME="txt_SmallDesc", ROWS=2, COLS=50>'.$_SESSION&#1111;'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&#1111;'bigdesc'])) echo '<TEXTAREA NAME="txt_BigDesc", ROWS=10, COLS=70>'.$_SESSION&#1111;'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&#1111;'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()
    &#123;
		document.getElementById('myForm').action = "run2.php?p=27";
		document.getElementById('myForm').submit();
    &#125;

    function preview()
    &#123;
		document.getElementById('myForm').action = "run2.php?p=24";
		document.getElementById('myForm').submit();
    &#125;

    function clear()
    &#123;
		document.getElementById('myForm').action = "run2.php?p=26";
		document.getElementById('myForm').submit();
    &#125;

    function sub()
    &#123;
		document.getElementById('myForm').action = "run2.php?p=25";
		document.getElementById('myForm').submit();
    &#125;
    </SCRIPT>
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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

did you read my posts?
Post Reply