Filled up $_FILES doesn't return file extension
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 |
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 '18 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 '18 at 0:18
add a comment |
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 '18 at 0:15
Phil
96.2k11136157
96.2k11136157
asked Nov 22 '18 at 0:00
GregGreg
217
217
"$_FILES['images']['name']
seems to be empty." 👈 it's pretty clear from yourvar_dump()
that it is far from empty
– Phil
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 0:18
add a comment |
1 Answer
1
active
oldest
votes
$_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 '18 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 '18 at 0:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$_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 '18 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 '18 at 0:23
add a comment |
$_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 '18 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 '18 at 0:23
add a comment |
$_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 '18 at 0:19
answered Nov 22 '18 at 0:11
RiggsFollyRiggsFolly
70.1k1864109
70.1k1864109
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 0:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 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 '18 at 0:18