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

PHP脚本将文件保存到错误的服务器文件夹,php,html,Php,Html,我有一个php脚本,它将svg和png保存到我的服务器上 当前,它将SVG文件保存到此文件夹: “/…已保存的客户文件” 它当前还将SVG和PNG文件保存到此位置: “../../customer design proof/” <?php require_once '../session.inc.php'; initSession(); if(!isset($_POST['output_svg']) && !isset($_POST['output_png']))

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

当前,它将SVG文件保存到此文件夹: “/…已保存的客户文件”

它当前还将SVG和PNG文件保存到此位置: “../../customer design proof/”

<?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>
问题:我需要它反向工作,将SVG和PNG保存到: “/…已保存的客户文件”

并仅将PNG保存到:“....../customer design Proof/”中

<?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>

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

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

我在帖子底部更新了这些常量,仍然不起作用。我不知道如何在这些注释部分添加lotsa代码。我没有在注释中添加很多代码。它变得不可读了。编辑你的问题并将其作为附录。好的,我试着简化它。不知道如何把它作为附录