0

I have recently been working on a project that uses a bit of PHP. I don't know whether my question is obvious to those who have loads of experience, but here goes.

I don't know how to get a response from an upload PHP I created. If I have a form, like so...

<!DOCTYPE html>
<html>
    <body>

        <form action="upload.php" method="POST" enctype="multipart/form-data">
            <label>Select image to upload:</label>
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit">
        </form>

    </body>
</html>

...and I have a PHP script that uploads these images to Cloudinary...

<?php

// Cloudinary init
require 'Cloudinary.php';
require 'Uploader.php';
require 'Api.php';
\Cloudinary::config(array( 
  "cloud_name" => "(cloud name)", 
  "api_key" => "(my key)", 
  "api_secret" => "(secret here)" 
));

// Uploads images to Cloudinary
\Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"]);

?>

...how can I make it so that when submitted, it adds the value of the photo's URL (which is stored in a hash) to a hidden input in another form? Thanks so much.

P.S. Sorry for asking such n00b-y questions! (I'm new here)

v3ryn3rdy
  • 21
  • 1
  • 5

1 Answers1

0

First, make sure the session has been started. This allows you to send data over the server with post and files.

session_start()

Then you can access your inputs by name using the post and file functions.

# form.php
<?php session_start() ?>
    <form action="after_form.php" method="POST" enctype="multipart/form-data">
    <input type="text" name="text-input" value="Moon Text" />
    <input type="hidden" name="some-input" value="xyyz" />
    <input type="submit" />
</form>

Then on your next page, once the form has been submitted,

# after_form.php
echo $_POST['text-input']; //prints "Moon Text"
echo $_POST['some-input']; //prints "xyyz"

Usually you would save these data somewhere though.

If on this same after_form.php page you have a new hidden input, you could do something like

<input type="hidden" name="file-path" <?php echo 'value="'.$_POST['data'].'"';?> />
Naltroc
  • 989
  • 1
  • 14
  • 34
  • Okay. But how does this return the hash that contains the picture's URL? – v3ryn3rdy Apr 25 '17 at 02:22
  • Where does the hash come form? If it is in `\Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"])` then you assign this to a variable, and that will hold the hash. Then do whatever you like with the variable. – Naltroc Apr 25 '17 at 13:37
  • Still getting an error: `Fatal error: Uncaught exception 'Cloudinary\Error' with message 'Missing required parameter - file' in /home/inciden4/public_html/report/Uploader.php:344 Stack trace: #0 /home/inciden4/public_html/report/Uploader.php(63): Cloudinary\Uploader::call_api('upload', Array, Array, NULL) #1 /home/inciden4/public_html/report/upload.php(21): Cloudinary\Uploader::upload(NULL, Array) #2 {main} thrown in /home/inciden4/public_html/report/Uploader.php on line 344`. I was trying to assign it to a variable. – v3ryn3rdy Apr 26 '17 at 13:44
  • You file isn't coming through - `with message 'Missing required parameter - file'`. Try doing `print_r($_FILES)` or `dd($_FILES)` to see what it churns out. – Naltroc Apr 26 '17 at 14:18
  • Huh. `print_r($_FILES)` returns `Array ( )`. Strange – v3ryn3rdy Apr 26 '17 at 14:24
  • Also, the images upload if I call the API like so: `\Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"]);`. However, I don't know how to have it return the hash. – v3ryn3rdy Apr 26 '17 at 14:26
  • Did you assign it to a var and tried to access it? Try to print it to see if you get the response (if the file was uploaded, you should see the full response) – Maor.G May 01 '17 at 08:27