5

I got form at frontend to submit stuff, wherein I need to check if file exists on submit.

Please note that the form submits array of files. It is failing the all logics I have been trying. Please notice the upload[] on second line of html.

HTML

<form method="post" id="upload-form" action="" enctype="multipart/form-data" >
<input type="file" multiple="true" name="upload[]">
<input type="submit" name="upload_attachments" value="UPLOAD">
<form>

PHP

if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
   }else{

   //do further validation   

   }

The code above shows 'file is not set !' in both cases, that is when I hit submit button without uploading a file and when I select to upload a file and hit the submit button.

What is happening here, am I even doing it right?

Basically I want to return a 'File Empty' message when someone hits the submit button without selecting a file to upload, and stop the further code execution.

0

4 Answers 4

6

$_FILES[<key>]['tmp_name'] - contains file path on server

$_FILES[<key>]['name'] - is just original file name

To address array of files file[] you need to fetch data by $_FILES[<key>][<property>][<id>]

try

if(!file_exists($_FILES['upload']['tmp_name'][0])) {

Manuals

http://www.php.net/manual/en/features.file-upload.php

http://www.php.net/manual/en/features.file-upload.multiple.php

Sign up to request clarification or add additional context in comments.

Comments

2

You are appending tmp_name and name. That is not needed. Just check uploaded file name like;

if(empty($_FILES['upload']['name'])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
}else{

   //do further validation   

}

or you can check file existence like;

if(!file_exists($_FILES['upload']['tmp_name'][0])) {

   $upload_error = 'file is not set !'; 
   return; //stop further code execution 
}else{

   //do further validation   

}

Your multiple file array format is like below;

Array
(
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

You can refer here

6 Comments

Second chunk - on hitting submit without file - shows the upload error but also the Warning: file_exists() expects parameter 1 to be a valid path, array given in......line 4. Hitting submit with file also gives the same result.
First chunk of your code - on hitting submit without file - is ignoring the if rule and goes to else rule, so no joy there. On hitting submit with the file - returns same result.
@HüseyinBABAL Your answer is completely invalid, check php.net/manual/en/features.file-upload.multiple.php
@Huseyin BABAL Yes, that is why I put an emphasis on upload[] and array part in the question, that is probably why all my other logics have failed till now. Checking on your new answer.
Thanks @mleko. @gurung I have made a mistake in my updated question. I have reverted to previous one. Warning: file_exists() expects parameter 1 to be a valid path, array given in......line 4 error given because it must be !file_exists($_FILES['upload']['tmp_name'][0]). See my updated answer.
|
0

You could use php's is_uploaded_file function.

Here's an excerpt from php's site about the function.

For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

http://www.php.net/manual/en/function.is-uploaded-file.php

1 Comment

I do not have a problem with function. Just how it handles the array input.
0

Try this

   if (empty($_FILES['upload']['name']))
   {
   // No file was selected for upload, your (re)action goes here
   }

  note: place this code above
   if(!file_exists($_FILES['upload']['tmp_name'].'/'.$FILES['upload']['name'])) {

1 Comment

@Ezhil This does not take the array matter in consideration. But thanks for the effort. This is resolved now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.