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 ?










share|improve this question
























  • "$_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















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 ?










share|improve this question
























  • "$_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













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 ?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • "$_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
















"$_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












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;





share|improve this answer























  • 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











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',
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
});


}
});














 

draft saved


draft discarded


















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








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;





share|improve this answer























  • 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















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;





share|improve this answer























  • 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













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;





share|improve this answer














$_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;






share|improve this answer














share|improve this answer



share|improve this answer








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 that move_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










  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo