我有一个php脚本read_write.php它用于将文件上传到谷歌云存储。该脚本运行良好,并成功将我的文件上传到谷歌云存储的 bubket。唯一的问题是,上传文件后,通过按上传,页面被重定向到谷歌使用的链接(请参阅下面的我的表格),
action="<?php echo $upload_url?>"
我不想被重定向到此链接。我想在文件重定向回我的 php 页面后,这是read_write.php 知道如何解决这个问题吗?这是我的代码:
<?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google'appengine'api'cloud_storage'CloudStorageTools;
$options = [ 'gs_bucket_name' => 'carb' ];
$upload_url = CloudStorageTools::createUploadUrl('/read_write', $options);
if(isset($_POST['do-upload']) AND $_POST['do-upload'] === "yes"){
$yesupload = $_POST['do-upload'];
preg_match("/yes/", "".$yesupload."");
$filename = $_FILES['testupload']['name'];
$gs_name = $_FILES['testupload']['tmp_name'];
$temp = explode(".",$_FILES["testupload"]["name"]);
$newfilename = substr(md5(time()), 0, 100) . '.' .end($temp);
move_uploaded_file($gs_name, 'gs://carb/'.$newfilename.'');
$object_image_file = 'gs://carb/'.$newfilename.'';
$object_image_url = CloudStorageTools::getImageServingUrl($object_image_file, ['size' => 400, 'crop' => true]);
mysql_query("UPDATE `myusers` SET `profile`= '" . mysql_real_escape_string($object_image_url) . "' WHERE `user_id`= " . (int)$session_user_id);
//---I use this header here, but there is no redirecton to this, after pressing upload button
header('Location: read_write.php');
exit();
}
//---displays the image i have upload
if(empty($user_data['profile'])=== false ){
echo ' <img src="', $user_data['profile'], '" alt="', 'Image 400k max">';
}else
echo'<img src="img/photo.jpg"/>';
<form class="SomeSpaceDude" action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
<p>Files to upload: </p> <br>
<input type="hidden" name="do-upload" value="yes">
<input class="SomeSpaceDude topcoat-button" type="file" name="testupload" >
<input class="SomeSpaceDude topcoat-button" type="submit" value="Upload">
</form>
你可以在ajax中尝试
//your php code before
<input type="hidden" name="do-upload" id="do-upload" value="yes">
<input class="SomeSpaceDude topcoat-button" type="file" id="testupload" name="testupload" >
<input class="SomeSpaceDude topcoat-button" type="button" onclick="save_form();" value="Upload">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function save_form(){
var testupload=$('#testupload').val();
var doupload=$('#do-upload').val();
var dataString="testupload="+testupload+"&do-upload="+doupload;
$.ajax({
type: "POST",
url: "<?php echo $upload_url ?>",
data: dataString,
cache: false,
success: function(){
location.reload();
}
});
}</script>
并从脚本中删除header('Location: read_write.php');