Filled up $_FILES doesn't return file extension
up vote
2
down vote
favorite
I wrote this short script to upload multiple files, based on an old topic from Stackoverflow : How do you loop through $_FILES array?
(I didn't reply into it as it's 7 years old... Let me know if I did wrong)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
So basically, I'm sending multiple files in $_FILES['images'] and I create unique names before upload (using time() + $i). I never get the file extension as $_FILES['images']['name'] seems to be empty. It's not though as Var_dump returns a complete array with everything I need :
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
I'd also need ['type'] to test the files but same thing : I Can't get to return the array's content.
Do you see something wrong in this script ?
php arrays file loops
add a comment |
up vote
2
down vote
favorite
I wrote this short script to upload multiple files, based on an old topic from Stackoverflow : How do you loop through $_FILES array?
(I didn't reply into it as it's 7 years old... Let me know if I did wrong)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
So basically, I'm sending multiple files in $_FILES['images'] and I create unique names before upload (using time() + $i). I never get the file extension as $_FILES['images']['name'] seems to be empty. It's not though as Var_dump returns a complete array with everything I need :
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
I'd also need ['type'] to test the files but same thing : I Can't get to return the array's content.
Do you see something wrong in this script ?
php arrays file loops
"$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from yourvar_dump()
that it is far from empty
– Phil
Nov 22 at 0:12
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I wrote this short script to upload multiple files, based on an old topic from Stackoverflow : How do you loop through $_FILES array?
(I didn't reply into it as it's 7 years old... Let me know if I did wrong)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
So basically, I'm sending multiple files in $_FILES['images'] and I create unique names before upload (using time() + $i). I never get the file extension as $_FILES['images']['name'] seems to be empty. It's not though as Var_dump returns a complete array with everything I need :
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
I'd also need ['type'] to test the files but same thing : I Can't get to return the array's content.
Do you see something wrong in this script ?
php arrays file loops
I wrote this short script to upload multiple files, based on an old topic from Stackoverflow : How do you loop through $_FILES array?
(I didn't reply into it as it's 7 years old... Let me know if I did wrong)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
So basically, I'm sending multiple files in $_FILES['images'] and I create unique names before upload (using time() + $i). I never get the file extension as $_FILES['images']['name'] seems to be empty. It's not though as Var_dump returns a complete array with everything I need :
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
I'd also need ['type'] to test the files but same thing : I Can't get to return the array's content.
Do you see something wrong in this script ?
php arrays file loops
php arrays file loops
edited Nov 22 at 0:15
Phil
94.8k11135154
94.8k11135154
asked Nov 22 at 0:00
Greg
153
153
"$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from yourvar_dump()
that it is far from empty
– Phil
Nov 22 at 0:12
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18
add a comment |
"$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from yourvar_dump()
that it is far from empty
– Phil
Nov 22 at 0:12
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18
"
$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from your var_dump()
that it is far from empty– Phil
Nov 22 at 0:12
"
$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from your var_dump()
that it is far from empty– Phil
Nov 22 at 0:12
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
$_FILES['images']['tmp_name']
does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so thatmove_upload_file()
will move the tmp file.
– RiggsFolly
Nov 22 at 0:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
$_FILES['images']['tmp_name']
does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so thatmove_upload_file()
will move the tmp file.
– RiggsFolly
Nov 22 at 0:23
add a comment |
up vote
3
down vote
accepted
$_FILES['images']['tmp_name']
does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so thatmove_upload_file()
will move the tmp file.
– RiggsFolly
Nov 22 at 0:23
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
$_FILES['images']['tmp_name']
does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;
$_FILES['images']['tmp_name']
does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;
edited Nov 22 at 0:19
answered Nov 22 at 0:11
RiggsFolly
68.9k1764109
68.9k1764109
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so thatmove_upload_file()
will move the tmp file.
– RiggsFolly
Nov 22 at 0:23
add a comment |
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so thatmove_upload_file()
will move the tmp file.
– RiggsFolly
Nov 22 at 0:23
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Thanks you @RiggsFolly, it's working that way. i guess I forgot the [$index]. But why can't I use foreach($_FILES['images']['name'] as $index => $tmpName) instead ? (I'm keeping your suggestion, but I'm being curious about it)
– Greg
Nov 22 at 0:22
Because you need that, to be the location and name of the TMP file so that
move_upload_file()
will move the tmp file.– RiggsFolly
Nov 22 at 0:23
Because you need that, to be the location and name of the TMP file so that
move_upload_file()
will move the tmp file.– RiggsFolly
Nov 22 at 0:23
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422118%2ffilled-up-files-doesnt-return-file-extension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"
$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from yourvar_dump()
that it is far from empty– Phil
Nov 22 at 0:12
That's why I'm asking for help. I tried a Foreach on $_FILES['images']['name'] instead of 'tmp_name' and nothing appear.
– Greg
Nov 22 at 0:18