我有以下代码,我使用Croppie
上传图片,裁剪并保存为base64数据,然后在表单提交,一个php文件,它运行两个参数,保存图像在特定目录中并重定向回页面。当它在localhost上时代码工作正常,但是当我托管它时,当我尝试访问php文件时,我得到403 Forbidden Error。它与权限无关,我认为这是一种安全措施。有没有另一种方法来解决这个问题,可能是通过将其更改为Ajax请求,如果有人可以帮助我代码我真的很感激。谢谢
HTML CODE:
<form type= "text" id="form" method="post">
<input type="file" id="upload" value="Choose a file">
<input type="hidden" id="imagebase64" name="imagebase64">
<a href="#" class="upload-result button button-primary full-width">Upload Photo</a>
</form>
</div>
<div id="upload-demo" style="float: right ;width:350px"></div>
</div>
<script type="text/javascript">
var USERid2 = null;
$( document ).ready( function () {
var em = '<?php echo $email?>';
$.ajax( {
type: "POST",
datatype: "text",
url: "/Proto/PHP/getUserID.php",
data: {
email: em
},
success: function ( data ) {
var obj = jQuery.parseJSON( data );
USERid2 = obj;
document.getElementById( 'form' ).action = '/Proto/uploadimage.php?USERid=' + USERid2 + '&currURL=' + window.location.href; // this sets form action, which includes the php file and parameters
}
} );
} );
$( document ).ready( function () {
var $uploadCrop;
function readFile( input ) {
if ( input.files && input.files[ 0 ] ) {
var reader = new FileReader();
reader.onload = function ( e ) {
$uploadCrop.croppie( 'bind', {
url: e.target.result
} );
$( '.upload-demo' ).addClass( 'ready' );
}
reader.readAsDataURL( input.files[ 0 ] );
}
}
$uploadCrop = $( '#upload-demo' ).croppie( {
viewport: {
width: 200,
height: 200,
type: 'circle'
},
boundary: {
width: 300,
height: 300
}
} );
$( '#upload' ).on( 'change', function () {
readFile( this );
} );
$( '.upload-result' ).on( 'click', function ( ev ) {
$uploadCrop.croppie( 'result', {
type: 'canvas',
size: 'original'
} ).then( function ( resp ) {
$( '#imagebase64' ).val( resp );
$( '#form' ).submit();
} );
} );
} );
</script>
PHP文件:
<?php
$id= $_GET['USERid'];
$url= $_GET['currURL'];
if(isset($_POST['imagebase64'])){
$data = $_POST['imagebase64'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$file = "../../Proto/SiteResources/ProfilePictures/" . $id . '.jpg';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
header("Location: $url");
}
?>