1

Form:

<form enctype="multipart/form-data" action="FileUpload.php" method="post">
                    <label for="file" class="myLabel">Select File</label>
                    <input type="file" id="file" name="fileUpload">
                    <input type="submit" class="submit" value="Upload File">

            </form>

php:

if(isset($_FILES['fileUpload'])){
        $uploadName = $_FILES['fileUpload']['name'];
        $uploadTmp = $_FILES['fileUpload']['tmp_name'];
        $fileSize = $_FILES['fileUpload']['size'];
        $fileType = pathinfo($uploadName, PATHINFO_EXTENSION);
        $uploadName = trim($uploadName, "." . $fileType);

        $uploadName = preg_replace("#[^a-z0-9.]#i","",$uploadName);//removes all spaces in their name

        $uploadName = mt_rand(100000,999999) . "____" . $uploadName;//generates random number in front of name so multiple files of the smae name can be uploaded

        if(($fileSize > 100024352638512 )){//check file is less than 10gb   
            die("Error - File to big");
        }

        if(!$uploadTmp){//checks to see if file is selected
            die("No File Selected, Please Upload Again");
        }else{
            move_uploaded_file($uploadTmp,"uploads/$uploadName");//puts file into uploads directory
        }

    $sql = $con->query("INSERT INTO uploads (ID, Time ,Name, Type, Owner) VALUE
    (NULL,CURRENT_TIMESTAMP(),'{$uploadName}','{$fileType}','{$_SESSION['UserID']}')")or die(mysqli_error($con));//inserts file into database

        header("Location:Profile.php");//sends you to profile page
        }

The code is executed to the end but the file is not uploaded to the database, I had the php working before but I changed how the input button worked and now it doesn't work. I will be grateful for any help as I am still quite a new developer.

Ben_W
  • 21
  • 1

2 Answers2

0

It appears you are trying to insert NULL into your ID column, which I am assuming is your primary key.

SQL Server, can't insert null into primary key field?

You cannot insert NULL into a primary key and rather, you should just skip trying to put anything into your ID column.

Community
  • 1
  • 1
Lucha Laura Hardie
  • 429
  • 1
  • 3
  • 10
0

Along the lines of Lucha Laura's answer - if the file is being uploaded and moved successfully, I would suspect that the values you are inserting violate the database fields' constraints.

Inserting NULL into the primary key is valid in MySQL, but the other fields may not be configured properly. Check the field types, lengths, and not_null requirements of the fields and make sure they accept the values you are sending. A simple way to preview the actual values is to send information to the error log with error_log().

Slightly unrelated - trim() doesn't do what you are trying to accomplish. Look into strstr() or preg_replace().

Wade
  • 129
  • 4