Check if image has been selected for upload












0














I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).



How can I run the upload image part only if the user selected an image?



I have tried:



if (!empty($_FILES['files']['name'])){ Upload the Image }

if (!empty($_FILES['files'])){ Upload the Image }

if (!empty($_FILES['files']['name'])) { Upload the Image }

if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }


This the form:



<input type="file" name="files" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">









share|improve this question






















  • Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
    – Martin Zeitler
    Nov 12 at 19:45
















0














I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).



How can I run the upload image part only if the user selected an image?



I have tried:



if (!empty($_FILES['files']['name'])){ Upload the Image }

if (!empty($_FILES['files'])){ Upload the Image }

if (!empty($_FILES['files']['name'])) { Upload the Image }

if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }


This the form:



<input type="file" name="files" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">









share|improve this question






















  • Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
    – Martin Zeitler
    Nov 12 at 19:45














0












0








0







I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).



How can I run the upload image part only if the user selected an image?



I have tried:



if (!empty($_FILES['files']['name'])){ Upload the Image }

if (!empty($_FILES['files'])){ Upload the Image }

if (!empty($_FILES['files']['name'])) { Upload the Image }

if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }


This the form:



<input type="file" name="files" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">









share|improve this question













I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).



How can I run the upload image part only if the user selected an image?



I have tried:



if (!empty($_FILES['files']['name'])){ Upload the Image }

if (!empty($_FILES['files'])){ Upload the Image }

if (!empty($_FILES['files']['name'])) { Upload the Image }

if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }


This the form:



<input type="file" name="files" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">






php image-uploading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 19:38









Beji

277




277












  • Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
    – Martin Zeitler
    Nov 12 at 19:45


















  • Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
    – Martin Zeitler
    Nov 12 at 19:45
















Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
– Martin Zeitler
Nov 12 at 19:45




Possible duplicate of $_FILES field 'tmp_name' has no value on .JPG file extension
– Martin Zeitler
Nov 12 at 19:45












1 Answer
1






active

oldest

votes


















1














Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:



if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}


Above solution code is based on your name attribute of input tag,



<input ... name="files" ... />


If it was <input ... name="files" ... /> then the if condition would be like this:



if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}




Sidenote: Use var_dump($_FILES); to see the complete array structure.






share|improve this answer























  • If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
    – Beji
    Nov 12 at 20:12










  • @Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
    – Rajdeep Paul
    Nov 12 at 20:15










  • thanks, I forgot to add [0]
    – Beji
    Nov 12 at 20:16











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268978%2fcheck-if-image-has-been-selected-for-upload%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









1














Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:



if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}


Above solution code is based on your name attribute of input tag,



<input ... name="files" ... />


If it was <input ... name="files" ... /> then the if condition would be like this:



if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}




Sidenote: Use var_dump($_FILES); to see the complete array structure.






share|improve this answer























  • If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
    – Beji
    Nov 12 at 20:12










  • @Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
    – Rajdeep Paul
    Nov 12 at 20:15










  • thanks, I forgot to add [0]
    – Beji
    Nov 12 at 20:16
















1














Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:



if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}


Above solution code is based on your name attribute of input tag,



<input ... name="files" ... />


If it was <input ... name="files" ... /> then the if condition would be like this:



if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}




Sidenote: Use var_dump($_FILES); to see the complete array structure.






share|improve this answer























  • If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
    – Beji
    Nov 12 at 20:12










  • @Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
    – Rajdeep Paul
    Nov 12 at 20:15










  • thanks, I forgot to add [0]
    – Beji
    Nov 12 at 20:16














1












1








1






Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:



if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}


Above solution code is based on your name attribute of input tag,



<input ... name="files" ... />


If it was <input ... name="files" ... /> then the if condition would be like this:



if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}




Sidenote: Use var_dump($_FILES); to see the complete array structure.






share|improve this answer














Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:



if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}


Above solution code is based on your name attribute of input tag,



<input ... name="files" ... />


If it was <input ... name="files" ... /> then the if condition would be like this:



if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}




Sidenote: Use var_dump($_FILES); to see the complete array structure.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 20:01

























answered Nov 12 at 19:47









Rajdeep Paul

15.9k31229




15.9k31229












  • If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
    – Beji
    Nov 12 at 20:12










  • @Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
    – Rajdeep Paul
    Nov 12 at 20:15










  • thanks, I forgot to add [0]
    – Beji
    Nov 12 at 20:16


















  • If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
    – Beji
    Nov 12 at 20:11










  • If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
    – Beji
    Nov 12 at 20:12










  • @Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
    – Rajdeep Paul
    Nov 12 at 20:15










  • thanks, I forgot to add [0]
    – Beji
    Nov 12 at 20:16
















If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
– Beji
Nov 12 at 20:11




If I use if(is_uploaded_file($_FILES['files']['tmp_name'])){...} the code got not called even if I select an image for upload.
– Beji
Nov 12 at 20:11












If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
– Beji
Nov 12 at 20:11




If I run var_dump($_FILES) without selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } }
– Beji
Nov 12 at 20:11












If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
– Beji
Nov 12 at 20:12




If I run var_dump($_FILES) with selecting a file: array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(63) "media.media.56852c99-74ee-4e6e-bf29-b37b0ce6bb46.normalized.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(19) "/home/tmp/phpvyOI7C" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(27933) } } }
– Beji
Nov 12 at 20:12












@Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
– Rajdeep Paul
Nov 12 at 20:15




@Beji, as I said in my updated answer; given the current <input ..> tag, your if block should be like this: if(is_uploaded_file($_FILES['files']['tmp_name'][0])){ ...
– Rajdeep Paul
Nov 12 at 20:15












thanks, I forgot to add [0]
– Beji
Nov 12 at 20:16




thanks, I forgot to add [0]
– Beji
Nov 12 at 20:16


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


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




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268978%2fcheck-if-image-has-been-selected-for-upload%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python