Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    upload file script = website A... file destination = website B... how?

    Because of server complications I have set up a Control Panel for a customer's website on my own website. I have their login information so if that is all I need to get this to work properly, that is not a problem.

    What I need to do is have the uploaded file (handled and processed through my website) sent to a directory on the customer's server. Right now, it is not allowing me to do that, obviously I have coded something wrong where it tries to upload it...

    Here is the part of my script where I believe the error is occuring:
    PHP Code:
      if (isset($_POST['submit'])) {
        require_once('../../../mysql-connect.php');

        $sql = "INSERT INTO salesTalk (fileName, fileSize, fileType, uploadDate) VALUES ('{$_FILES['upload']['name']}', {$_FILES['upload']['size']}, '{$_FILES['upload']['type']}', NOW())";
        $result = @mysql_query ($sql);

        if ($result) {
          $extension = explode ('.', $_FILES['upload']['name']);
          $iid = mysql_insert_id();
          $filename = $iid . '.' . $extension[1];

          if (move_uploaded_file($_FILES['upload']['tmp_name'], "http://www.my.customer.org/salesTalk/$filename")) {
            echo '<span class="textNorm">Thank you, the file has been uploaded. </span>';
          } else {
            echo '<span class="error">The file could not be stored. </span>';

            $sql = "DELETE FROM salesTalk WHERE issueId = $iid";
            $result = @mysql_query ($query);
          }
        } else {
          echo '<span class="error">We\'re sorry, a system error occurred. Please try again. </span>';
        }

        mysql_close();

      }
        
    echo '
                    <form enctype="multipart/form-data" action="'; ?><?php echo $_SERVER['PHP_SELF']; ?><?php echo '" method="post">
                      <input type="hidden" name="MAX_FILE_SIZE" value="524288">
                      <table cellpadding="0" cellspacing="0" align="center">
                        <tr>
                          <td align="left" class="textNorm">File: </td>
                          <td align="left" class="textNorm"><input type="file" name="upload" size="16" maxlength="20" value="'
    ?><?php if (isset($_POST['firstName'])) echo $_POST['firstName']; ?><?php echo '" /></td>
                        </tr>
                        <tr>
                          <td colspan="2" class="textNorm" align="center">
                            <input type="submit" name="submit" value="Upload" />
                          </td>
                        </tr>
                      </table>
                    </form>
                  </td>
                </tr>
              </table>
            </td>
          </tr>'
    ;
    include(
    'inc-bin/footer.inc');
    Thank you for any and all help on this matter!

    edit: I think (one of) the problem(s) was that the directory permissions were not set correctly. The directory is now chmoded to 777 but I'm still getting the error.
    Last edited by dotted line; 01-17-2004 at 04:51 PM.
    someone blew a wish
    it flew in my window

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if  (move_uploaded_file($_FILES['upload']['tmp_name'],
     
    "http://www.my.customer.org/salesTalk/$filename")) { 
    This line should not work. AFAIK, you can't move files to URL destinations. You rather need a filesystem path, like

    /document_root/salesTalk/$filename

    Try out both relative and full paths, and see if this measure makes your app work.
    De gustibus non est disputandum.

  • #3
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    But the destination is on a completely different server, how would I specify the filepath to that?
    someone blew a wish
    it flew in my window

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Location
    Georgia
    Posts
    306
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FULL path
    ~Designer's Toolz~

  • #5
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your help, both of you.

    Would the full path begin with http:// ?
    I've never done this before so sorry for the stupid questions.
    someone blew a wish
    it flew in my window

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry dotted line, I did not read your original post closely enough. Anyway, you can't "move" uploaded files to other servers. At least not with move_uploaded_file(). What you need is to re-upload the file to the destination server. That also requires a script to be on the destination server which handles the incoming file data.

    So now you have the problem how to send the file to another server. You can use FTP functions from within PHP, if you happen to have a FTP account for the target server. Or you use a command line application by exec(), though that makes your script not very portable.
    Additionally, you could open a socket (with fsockopen()) and send the file to the destination server by establishing a HTTP connection and sending it via POST. Another option would be to use the cURL extension.

    All what I've said is essentially doable only if the necessary requirements are met. That is to have either FTP, sockets or cURL installed at your "a" server, and the ability to have a script running at "b" server. If you tell us what of these options your server architecture supports, we can give more detailed help.
    De gustibus non est disputandum.

  • #7
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just noticed that my php.ini file has the temp upload dir commeneted out:
    ;upload_tmp_dir =

    So actually, that appears to be the underlying problem. Any idea what I should do about this, or should I just email the hosting company?

    After that is worked out, FTPing the files would work fine!
    someone blew a wish
    it flew in my window

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The directory for storing the uploads temporarily must be set, and you must have read/write access to it. Check your open_basedir setting as well. Either you do these adjustments yourself and restart the server, or you have to ask your host to do this. They should, if they sold you a hosting packages that includes standard PHP functionality.

    There are a couple of other settings that affect file uploads too. Be sure to check the configuration notes mentioned on http://us2.php.net/manual/en/features.file-upload.php and probably include them in your request to the host as well.
    De gustibus non est disputandum.

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Heh, talk about quick replies. I was just about to say what mordred said about using ftp and such, good job I pressed the preview message button instead of reply

    It's a good job you can't upload files in the way you tried dotted line, it would create chaos You'd be able to upload all sorts of files to other sites, like replacing yahoo's index page with your own index page heh

  • #10
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh, yeah Nightfire, that would be bad. I pretty much assumed that was part of the problem from the start, I just didn't know how to correct it.

    Mordred, or anyone else who wants to help:

    I have gotten uploads to work. Now I need to ftp them to another server. I have spent the past hour or so trying to do this but it's done nothing but give errors and make me want to go to bed.

    Thanks for the help, I'll check back in the morning.
    someone blew a wish
    it flew in my window

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I myself have not much experience with FTP functions in PHP (seldom needed them), but this modified script from the manual works alright for me:

    PHP Code:
    $ftp_server         'server.example.com';
    $ftp_user_name         'username';
    $ftp_user_pass         '******';
    $source_file         'file.txt'// filesystem path on first server
    $destination_file     'file.txt'// filesystem path on second server        

    // set up basic connection 
    $conn_id ftp_connect($ftp_server); 

    // login with username and password 
    $login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass); 

    // check connection 
    if ((!$conn_id) || (!$login_result)) { 
        echo 
    "FTP connection has failed!<br>"
        echo 
    "Attempted to connect to $ftp_server for user $ftp_user_name<br>"
        exit; 
    } else { 
        echo 
    "Connected to $ftp_server, for user $ftp_user_name<br>"


    // upload the file (either in ASCII or Binary mode)
    $upload ftp_put($conn_id$destination_file$source_fileFTP_ASCII); 

    // check upload status 
    if (!$upload) { 
        echo 
    "FTP upload has failed!"
    } else { 
        echo 
    "Uploaded $source_file to $ftp_server as $destination_file"
    }

    // close the FTP stream 
    ftp_close($conn_id); 
    De gustibus non est disputandum.

  • #12
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh. Ok, let's nix the ftp connection.

    What am I doing wrong here...

    fopen("ftp://user:pass@host/path/" . $filename, "w");

    This creates a blank file with the correct name. What mode do I have to use so that it sends the actual file? I have a feeling I'm not doing this right at all.
    someone blew a wish
    it flew in my window

  • #13
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Although you've got ftp in the url, you're not actually doing as me and mordred said You need to use the ftp functions php has. Use mordreds's script, fill in with your details and merge it in with your script. More info on ftp functions

  • #14
    New to the CF scene
    Join Date
    Sep 2003
    Location
    delaware
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't use the ftp commands in php with my host. Sorry for not being clear on that. What about fopen() or something else?
    someone blew a wish
    it flew in my window

  • #15
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Can't use fopen or anything as you'd not have the right permissions. Do you have sockets available? If not then there's no other way to do it apart from manually ftp'ing


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •