www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35

Thread: Storing a post image.

  1. #1
    Join Date
    Jun 2004
    Posts
    1,299

    Storing a post image.

    Hi,

    I think the best way is to start with what I am doing. I have a form on my website and they fill it in and upload a picture. The main problem here is that i need to upload a the picture to there website (different domain & server). There is quite a big permisions problem with this.

    As a work around what I done was direct the form directly to there domain then put my script that processed it on there server. Thing is im not really that happy with it because any changes means i need to change the file on there server.

    Is that any way I can perhaps process it on my server put the image into a session or something that go to there server and upload the image. I dont mind a file on there server but i would like to be able to validate the image (size) and the fields on my server still.

    Any ideas would be great.

    Thanks
    k0r54

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    i think any solution is gonna be dirty, but what you could do is trigger a script on the remote server (after the upload is completed), that copies it form your server to that one.

  3. #3
    Join Date
    Jun 2004
    Posts
    1,299
    thats not a bad idea. I know its a little dirty and im not really that happy I have to do it like that. Again though, wont I still get a permision problem just the other way round?

    Thanks
    Adam

  4. #4
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    nopes. it's just a matter of retrieving an image from a server
    if permission problems would be an issue, then your browser would have the same problem.

  5. #5
    Join Date
    Jun 2004
    Posts
    1,299
    ok then,

    Well i suppose the next step now is how can i trigger a script from another server?

    Thanks
    k0r54

  6. #6
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    for example:
    http://www.php.net/fopen

    you could even make it give output that you can read with the upload-script, to determine whether is was succesful or not
    Last edited by themarty; 05-23-2006 at 06:04 AM. Reason: additional info

  7. #7
    Join Date
    Jun 2004
    Posts
    1,299
    I have read through it, i cant say i fully understand it but i get the drift.

    So i would run that from there server and i would be able to copy the image?

    Any help on that function would be great.

    Thanks
    k0r54

  8. #8
    Join Date
    Jun 2004
    Posts
    1,299
    Hi Well the way i currently validate my form is to direct the form to itself and at the top of the page use this code: -

    PHP Code:
    // Check the validation
    if(isset($_POST) && is_array($_POST) && ($_POST['val'] == 'true')) { 
        
      
    // Validation via validation page
      
    $error_msg validate(); 
        
      if(
    $_SESSION['VAL_TRUE'] == true) { 
            
        
    // Put all the post data into a session //
        
    $_SESSION['POST_DATA'] = validate();
        
    header('Location:' FILENAME_CHECKOUT_INVOICE '.php');
            
      } else {
        
    $errors true
      }

    The validate functions checks all the post variables and then returns an error msg which i display further on but if its fine and everything has valiadate properly i go to the header location.

    How could i put that function into this situation?

    Thanks
    k0r54

  9. #9
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    this is how you could do it:

    to trigger the script:
    Code:
    fopen("http://www.remoteserver.com/scriptToBeTriggered.php?file=".urlencode("uploadedImg.jp"), "r");
    -------

    scriptToBeTriggered.php
    Code:
    <?php
     // copied from the usercomments on http://www.php.net/fopen
    function download($file_source, $file_target) {
           $rh = fopen($file_source, 'rb');
           $wh = fopen($file_target, 'wb');
           if ($rh===false || $wh===false) {
    // error reading or opening file
               return true;
           }
           while (!feof($rh)) {
               if (fwrite($wh, fread($rh, 1024)) === FALSE) {
                       // 'Download error: Cannot write to file ('.$file_target.')';
                       return true;
                   }
           }
           fclose($rh);
           fclose($wh);
           // No error
           return false;
       }
    
    if (download("http://www.yourserver.com/uploaddir/".urldecode($_GET['file']), "whereverYouWantIt")
    {
       echo "Yeah!";
    }
    else
    {
       echo "Damn!";
    }
    ?>
    That's basically the idea
    Of course you need to add some security precautions here and there, so other people can not abuse the script

  10. #10
    Join Date
    Jun 2004
    Posts
    1,299
    Ahhh ok great

    I will try that. Thanks very much

  11. #11
    Join Date
    Jun 2004
    Posts
    1,299
    Hi,

    Using the fopen is there a way i can pull pack some code. You have echo Yeah and echo damn but neither of them actually display anything on the page.

    so you know what im doing.

    I have simple (at the moment) got: -
    page 1
    PHP Code:
    fopen("http://www.xxxxx.com/ImgUploadfile.php?file=" urlencode("www.myserver.com") . "&pID=" urlencode("an id"), "r"); 
    http://www.xxxxx.com/ImgUploadfile.php
    PHP Code:
    if (strpos(urldecode($_GET['file']), "www.myserver.com") === true) {
      echo 
    "Thank you";                    
    } else {
      echo 
    "You do not have permision to this file";

    This display nothing??

    Thanks
    k0r54

  12. #12
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    the download script can only download remote files to the local server on which the script is running, so what you have to do is put the script on the server where on which you want to download the files. This is in your case the remote server.
    Then, when the script is working OK, you access it from your own server using for example fopen.

    So,
    REMOTE SERVER A:
    - /htdocs/download.php
    - /htdocs/images/

    LOCAL SERVER B:
    - htdocs/activate.php
    - htdocs/uploaded_files/

    download.php contains the script that does the downloading:
    PHP Code:
    if (download("http://SERVER B/uploaded_files/".urldecode($_GET['file']), "images/".urldecode($_GET['file']))
    {
       echo 
    "Yeah!";
    }
    else
    {
       echo 
    "Damn!";

    activate.php is the script that you trigger after the file is uploaded and does the following:
    PHP Code:
    fopen("http://SERVER A/download.php?file=".$_FILES['file']['name'], "r"); 
    in this situation i've assumed that all the uploaded files are stored in the dir /uploaded_files

    check also http://www.php.net/fopen to see how to retrieve the response Yeah! or Damn

  13. #13
    Join Date
    Jun 2004
    Posts
    1,299
    Hi,

    Thanks for the break down. I went to the fopen section on php.net before i done posted back up here but cannot seem to see how to retrieve the response?

    Thanks
    k0r54

  14. #14
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by themarty
    i think any solution is gonna be dirty, but what you could do is trigger a script on the remote server (after the upload is completed), that copies it form your server to that one.
    Ok... but I believe you've got it wrong. The script on the remote server could be as simple as:
    PHP Code:
    <?php require_once('http://www.yourdomain.com/yourscript'); ?>
    No more code than that is required on their server. On your server have a file called yourscript (no extension needed) that contains your PHP script. Now you have complete control of everything directly from a script on your own server.

  15. #15
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    basically everything is already in the download function.

    so, try this for example:

    PHP Code:
    <?php
    if ($rh fopen("http://www.webdeveloper.com/forum/showthread.php?p=576869""r"))
    {
       echo 
    "Retrieved url succesfully!<br><br>";
       
    $content "";
       while (!
    feof($rh))
       {
           
    $content .= fread($rh1024);
       }
       echo 
    "Sourcecode:<pre>".htmlspecialchars($content)."</pre>";
    }
    else
    {
       echo 
    "Retrieving the url failed";
    }
    ?>
    Last edited by themarty; 05-31-2006 at 10:43 AM. Reason: posted some crap

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles