PHP脚本将文件保存到错误的服务器文件夹

时间:2014-02-10 03:48:31

标签: php html

我有一个php脚本,可以将svg和png保存到我的服务器上。

目前它将SVG文件保存到此文件夹: '/ ..保存-客户文件'

它目前还将SVG和PNG文件保存到此位置: '../../客户设计样张/'

问题:我需要它反对,将SVG和PNG保存到: '/ ..保存-客户文件'

仅将PNG保存为:'../../ 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>

1 个答案:

答案 0 :(得分:0)

也许是因为你使用了错误的常量?

define('DIR_PATH_PNG', '../saved-design-proofs/');
                ^^^^---- has _PNG
$fp = fopen(DIR_PATH.$file, 'w+');
                    ^---- **NO** _PNG