How to Check Whether the User Uploaded a File in PHP

How to check whether the user uploaded a file in PHP?

You can use is_uploaded_file():

if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo 'No upload';
}

From the docs:

Returns TRUE if the file named by
filename was uploaded via HTTP POST.
This is useful to help ensure that a
malicious user hasn't tried to trick
the script into working on files upon
which it should not be working--for
instance, /etc/passwd.

This sort of check is especially
important if there is any chance that
anything done with uploaded files
could reveal their contents to the
user, or even to other users on the
same system.

EDIT: I'm using this in my FileUpload class, in case it helps:

public function fileUploaded()
{
if(empty($_FILES)) {
return false;
}
$this->file = $_FILES[$this->formField];
if(!file_exists($this->file['tmp_name']) || !is_uploaded_file($this->file['tmp_name'])){
$this->errors['FileNotExists'] = true;
return false;
}
return true;
}

PHP check if there is a file selected for upload

Use the $_FILES array and the UPLOAD_ERR_NO_FILE constant:

if(!isset($_FILES['file_upload']) || $_FILES['file_upload']['error'] == UPLOAD_ERR_NO_FILE) {
echo "Error no file selected";
} else {
print_r($_FILES);
}

You can also check UPLOAD_ERR_OK which indicates if the file was successfully uploaded (present and no errors).

Note: you cannot use empty() on the $_FILES['file_upoad'] array, because even if no file is uploaded, the array is still populated and the error element is set, which means empty() will return false.

How to test if a user has SELECTED a file to upload?

You can check this with:

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

Or you can use a javascript construction that only enables the upload/submit button whenever the upload field has a value other then an empty string ("") to avoid submission of the form with no upload at all.

check if user uploaded a file in PHP

Check this

            <form enctype="multipart/form-data" method="POST">
Choose a file to upload:
<input type="file" name="upload1[]" multiple />
<br />
<input type="submit" value="Upload File" name="submit" />
</form>
<?php

if (isset($_POST['submit'])) {

if (isset($_FILES['upload1'])) {


$counter = count($_FILES['upload1']['name']);
for ($i = 0; $i < $counter; $i++) {

if (is_uploaded_file($_FILES['upload1']['tmp_name'][$i])) {
echo $_FILES['upload1']['tmp_name'][$i] . ' - ok <br>';
}

}

}
}


?>

Check whether file is uploaded

if(!file_exists($_FILES['name_of_field']['tmp_name']) 
{
# No file uploaded
}

Check file extension in upload form in PHP

Using if( $ext !== 'gif') might not be efficient. What if you allow like 20 different extensions?

Try:

$allowed = array('gif', 'png', 'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
echo 'error';
}

How to Check if the the user has uploaded a file or not in Javascript

Just get your file input by id and check if it's value is empty :

if(document.getElementById('file').value=='') {//something here }

How to check if a user uploaded a file using PHP?

Try this:

$file = $fu['filepath'].$fu['filename'];
$handle=@fopen($file,"r");
if($handle) {
while($row = fgetcsv($handle, 1024)){
echo "<input type='checkbox' name='receptionts[]' checked='checked' value='".$row[1] ."' /> ".$row[0]." <br />";
}
} else {
// File doesn't exist. do something.
}


Related Topics



Leave a reply



Submit