-1

I want to check if a file has been selected for upload when submitting my edit record form. Currently it's not working as it's overwriting the current filepath with a blank record instead of doing nothing. I have tried searching other questions related to this but none of the answers help me.

if($_FILES['healthandsafetyupload']['name'] != "") {


//H&S Policy File Upload
    if (count($_FILES['healthandsafetyupload']['name']) > 0) {
        $hsInt = count($_FILES['healthandsafetyupload']['name']);

    //Loop through each file
    for ($i = 0; $i < count($_FILES['healthandsafetyupload']['name']); $i++) {
        //Get the temp file path
        $tmpFilePath = $_FILES['healthandsafetyupload']['tmp_name'][$i];

        //Make sure we have a filepath
        if ($tmpFilePath != "") {

            //save the filename
            $shortname = $_FILES['healthandsafetyupload']['name'][$i];
            $fileExt = get_extension($shortname);


            //save the url and the file
            $hasfilePath = "uploads/has_" . random_string(14) . "." . $fileExt;

            //Upload the file into the temp dir
            move_uploaded_file($tmpFilePath, $hasfilePath);

            }
        }
    } 
}   

$sql1 = "UPDATE `subcontractor_qs` SET 
healthandsafetyupload = '" . mysql_real_escape_string($hasfilePath) . "' WHERE subcon_id = $subcon_id";
$query1 = mysql_query($sql1) or die(mysql_error());
Ross
  • 66
  • 12
  • 2
    Possible duplicate of [How to test if a user has SELECTED a file to upload?](https://stackoverflow.com/questions/2958167/how-to-test-if-a-user-has-selected-a-file-to-upload) – Thielicious Oct 20 '17 at 09:18
  • None of those answers solve my problem. – Ross Oct 20 '17 at 09:41
  • Your IF clause is okay, it's just your update query that's outside of it, so it gets triggered even if no file is selected – dkasipovic Oct 20 '17 at 12:04

2 Answers2

0
if($_FILES['image']['size']==0 && $_FILES['image']['error']==0){
    //Your code
}
waka
  • 3,362
  • 9
  • 35
  • 54
V5NEXT
  • 1,907
  • 4
  • 19
  • 38
0

This solved it for me.

if ($hasfilePath != "") {
    $sql1 = "UPDATE `subcontractor_qs` SET 
    healthandsafetyupload = '" . mysql_real_escape_string($hasfilePath) . "' WHERE subcon_id = $subcon_id";
    $query1 = mysql_query($sql1) or die(mysql_error());
}
Ross
  • 66
  • 12