形式:
<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
}
代码执行到最后但文件没有上传到数据库,我之前有php工作,但我改变了输入按钮的工作方式,现在它不起作用。我会感激任何帮助,因为我还是一个新开发者。
答案 0 :(得分:0)
看来你正试图在你的ID列中插入NULL,我假设它是你的主键。
SQL Server, can't insert null into primary key field?
您不能将NULL插入主键,而应该跳过尝试将任何内容放入ID列。
答案 1 :(得分:0)
按照Lucha Laura的回答 - 如果文件正在上传并成功移动,我会怀疑您插入的值违反了数据库字段&#39;约束
在主键中插入NULL在MySQL中有效,但其他字段可能配置不正确。检查字段的字段类型,长度和not_null要求,并确保它们接受您要发送的值。预览实际值的一种简单方法是使用error_log()
将错误日志发送到错误日志。
略有不相关 - trim()
并没有做你想要完成的事情。查看strstr()
或preg_replace()
。