Get the name of the files sent by the form

Asked

Viewed 31 times

0

I’m trying to get the name of the files after sending the form:

add_action('gform_after_submission_5', function ($entry, $form) {

    $file_url = $entry['5'];

    $filename = basename($file_url);

}, 10, 2);

When giving a var_dump $file_url; he returns it to me (I’m uploading only one file for now):

string(72) "["http://winds.local/wp-content/uploads/2016/04/stabbing312.jpg"]"

And by giving a var_dump $filename; return this:

string(17) "stabbing313.jpg"]"

How do I make $filename not catch quotes and square bracket?

UPDATING

In doing so:

$foo = explode(",", $file_url[0]);

foreach ($foo as $teste) {
    echo $teste.'<br>';
}

It even returns the Urls with the brackets:

["http://winds.local/wp-content/uploads/2016/04/WOLVERINE-THE-X-MEN-11-AVX-Tie-In10.jpg" "http://winds.local/wp-content/uploads/2016/04/stabbing324.jpg"]

  • What? Quotes and brackets are not the result of the "Print" variable with vardump?

  • @user5978 the problem is that basename is considering the quotes and conchete at the end as part of the URL. It should stop in jpg, but it includes "]

2 answers

1

Instead of var_dump. Try doing this:

echo $file_url[0];

If the output you posted is complete, it means there is an array, containing a string

But if the string content is: "["http://winds.local/wp-content/uploads/2016/04/stabbing312.jpg"]". You can try to remove the characters ",[ and ] as follows:

$file_url = substr($file_url,2,strlen($file_url)-2);
$file_url = substr($file_url,0,strlen($file_url)-2);

echo $file_url;

You can also use eval, concatenating into the string an assignment into an array, then taking the value in the form I initially suggested, but I don’t think that’s very safe. Best extract the characters in the "arm".

  • OK, returned ["http://winds.local/wp-content/uploads/2016/04/stabbing320.jpg"]

  • with echo? catching the array’s Dice?

  • No, just the string above.

  • I checked now, if the content is text, var_dump has the same output.

  • When I play $file_url[0] on a variable and give it a foreach, it returns invalid argument

  • Okay, I modified the answer. I added the solution to what appears to be the real problem.

Show 1 more comment

0

if it’s the case of an upload I always get it this way:

$_FILES['arquivo']['name']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.