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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP script to saving files to the wrong server folder

    I have a php script that saves both an svg and a png to my server.

    Currently it saves the SVG file to this folder: '/..saved-customer-files'

    It ALSO currently saves both the SVG and PNG file to this location: '../../customer-design-proofs/'

    The Problem: I need it to work opposite, save the SVG and PNG to: '/..saved-customer-files'

    And save Only the PNG to: '../../customer-design-proofs/'

    PHP Code:
    <?php

    require_once '../session.inc.php';

    initSession();


    if(!isset(
    $_POST['output_svg']) && !isset($_POST['output_png'])) {
        die(
    'post fail');
    }

    $file '';

    $suffix = isset($_POST['output_svg'])?'.svg':'.png';

    if(isset(
    $_POST['filename']) && strlen($_POST['filename']) > 0) {
        
    //$file = $_POST['filename'] . $suffix;
        
    $un UniqueName();
        
    $_SESSION['name'] = $un;
        
    $file =  $un $suffix;
    } else {
        
    //$file = 'image' . $suffix;
        
    $un UniqueName();
        
    $_SESSION['name'] = $un;
        
    $file =  $un $suffix;
    }

    if(
    $suffix == '.svg') {
        
    $mime 'image/svg+xml';
        
    $contents rawurldecode($_POST['output_svg']);
    } else {
        
    $mime 'image/png';
        
    $contents $_POST['output_png'];
        
    $pos = (strpos($contents'base64,') + 7);
        
    $contents base64_decode(substr($contents$pos));
    }


    /* Sets Path for SVG file */
    define('DIR_PATH_SVG''../saved-customer-files/');
    $fp fopen(DIR_PATH_SVG.$file'w+');
    $temp=fwrite($fp$contents);

    /* Sets Path for PNG file */
    define('DIR_PATH_PNG''../../customer-design-proofs/');
    $fp fopen(DIR_PATH_PNG.$file'w+');
    $temp=fwrite($fp$contents);



    fclose($fp);

    if (isset(
    $_POST['output_svg'])) {
        
    $svg_contents $_POST['svg'];
        
    $pos = (strpos($svg_contents'base64,') + 7);
        
    $svg_contents base64_decode(substr($svg_contents$pos));
        
    file_put_contents(DIR_PATH_SVG $_SESSION['name'] . '.svg'$svg_contents);
    }

    if (isset(
    $_POST['output_png'])) {
        
    $png_contents $_POST['output_png'];
        
    $pos = (strpos($png_contents'base64,') + 7);
        
    $png_contents base64_decode(substr($png_contents$pos));
        
    file_put_contents(DIR_PATH_PNG $_SESSION['name'] . '.png'$png_contents);
    }

    function 
    UniqueName() {
        
    $random_id_length 10
        
    $rnd_id crypt(uniqid(rand(),1)); 
        
    $rnd_id strip_tags(stripslashes($rnd_id)); 
        
    $rnd_id str_replace(".","",$rnd_id); 
        
    $rnd_id strrev(str_replace("/","",$rnd_id)); 
        
    $rnd_id substr($rnd_id,0,$random_id_length);
        return 
    $rnd_id;
    }


    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
    </head>
    <body>
    <script>
    window.parent.svgEditor.savedFileName = '<?php echo $_SESSION['name']; ?>';
    </script>
    </body>
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The easiest thing to do is simply swap the defined constant names.
    If it works exactly how you want except the locations to swap, then you simply change the locations and in this case, you can simply swap the defined names.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The easiest thing to do is simply swap the defined constant names.
    If it works exactly how you want except the locations to swap, then you simply change the locations and in this case, you can simply swap the defined names.
    Already tried that. Makes sense to me, but there is no difference, it still saves the exact same locations and methods as before.

    It must be somewhere else in the code that I obviously do not understand.

  • #4
    New Coder
    Join Date
    Feb 2014
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I do see that if I comment out this, it has No effect on the current result.

    PHP Code:
    /*
    if (isset($_POST['output_svg'])) {
        $svg_contents = $_POST['svg'];
        $pos = (strpos($svg_contents, 'base64,') + 7);
        $svg_contents = base64_decode(substr($svg_contents, $pos));
        file_put_contents(DIR_PATH_SVG . $_SESSION['name'] . '.svg', $svg_contents);
    }
    */ 


  •  

    Tags for this Thread

    Posting Permissions

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