duanjuelian4640 2017-05-19 19:24
浏览 71
已采纳

Canvas不会保存服务器端PHP

I have a canvas which I need to save to a directory and store the URL in a database.

When I save the file without storing the URL in the database it works fine, and vice versa.

However, when I put the two together and specify the PHP file through AJAX, for some reason it doesn't recognise the session variable?

When I try to call the "success" on AJAX, nothing shows up. I get no response.

This could possibly be an easy fix! I think I've been staring at this code for too long.

JavaScript:

function doodleSave() {
  var canvas = document.getElementById("doodle-canvas");
  var canvasData = canvas.toDataURL("image/png");

  $.ajax({
    url:'doodleupload.php', 
    type:'POST', 
    data:{ data:canvasData },
    success: function(response){
        alert(response);
        //echo what the server sent back...
    }
  });
}

PHP:

<?php
  session_start();

  /* AUTOMATED VARIABLES */
  $url             = md5(uniqid(rand(), true));
  $unique_user_id  = $_SESSION['unique_user_id'];
  $unique_post_id  = md5(uniqid(rand(), true));
  $timestamp       = time();
  $nature          = "doodle";
  $imageUrl        = $upload_dir.$url.'.png';

  $upload_dir = "images/external/doodles/";
  $img = $_POST['data'];
  $img = substr($img,strpos($img,",")+1);
  $data = base64_decode($img);
  $file = $upload_dir . $url . ".png";
  $success = file_put_contents($file, $data);

  echo $success ? $file : 'Unable to save the file.';

  require_once 'php/connect.php';

  try
  {

    $stmt = $pdo->prepare("INSERT INTO posts (unique_user_id, unique_post_id, nature, image_url, timestamp) VALUE (:unique_user_id, :unique_post_id, :nature, :image_url, :timestamp)");
    $stmt->bindParam(":unique_user_id",$unique_user_id);
    $stmt->bindParam(":unique_post_id",$unique_post_id);
    $stmt->bindParam(":nature",$nature);
    $stmt->bindParam(":image_url",$imageUrl);
    $stmt->bindParam(":timestamp",$timestamp);

    if($stmt->execute())
    {
      echo "File in database";
    }
    else
    {
      echo "Not in database";
    }
  }
  catch(PDOException $e){
    echo $e->getMessage();
  }

?>
  • 写回答

1条回答 默认 最新

  • dongyi0114 2017-05-19 20:36
    关注

    Move $upload_dir at the top, as you are calling it before you initialize it.

    $upload_dir = "images/external/doodles/";
    $url             = md5(uniqid(rand(), true));
    $unique_user_id  = $_SESSION['unique_user_id'];
    $unique_post_id  = md5(uniqid(rand(), true));
    $timestamp       = time();
    $nature          = "doodle";
    $imageUrl        = $upload_dir.$url.'.png';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)