0

I have a php script read_write.php that it is used to upload files to google cloud storage. The script works fine and uploads successfully my files, to my bubket at google cloud storage. The only problem is that after files uploaded, by pressing upload, the page is redirected to a link used by google (see at my form bellow),

action="<?php echo $upload_url?>"

I dont want to be redirected to this link. I want after the file to be redirected back to my php page which is read_write.php Any idea how to fix this? This is my code:

<?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>
user2491321
  • 673
  • 10
  • 32
  • That's odd. the first parameter to createUploadUrl, $success_path, is supposed to be for just that. What Google URL are you being redirected to? – Brandon Yarbrough Jan 05 '15 at 18:20
  • it redirects me to a url : /_ah/upload/AMmfu6aUVTgiLLMe9Vn...From what I have understand in order to upload an image to google cloud storage, this url used for the upload. But my problem is that I dont want to stay to this empty page that this url redirects me. I want to be redirected back to same page – user2491321 Jan 05 '15 at 18:29

1 Answers1

0

you might try in 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>

and remove header('Location: read_write.php'); from your script

napster3world
  • 366
  • 1
  • 2
  • 9