Jump to content

PHP script to saving files to the wrong server folder


mahogan

Recommended Posts

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

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>
Link to comment
Share on other sites

This is just a guess, but have you looked closely at the following lines:

/* 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);
 
 
It looks like you're writing the contents to a file...and then you write the contents again with file_put_contents() later.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.