1, Upload file front-end code:
Add the following code to the netdisk_html.php file
<?php <form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form>
2, From the front-end code, we can know that through the post method When submitting a file upload request, don’t forget to add enctype="multipart/form-data"
On the index.php interface, you only need to operate the uploaded file to upload and write successfully. The database can be displayed on the front end
<?php //获取post提交的上传文件的信息 $uploadfile=isset($_FILES['file'])?$_FILES['file']:""; //上传文件功能 if(!empty($uploadfile)){ if($uploadfile['error']==0){ //上传成功 $uploadfile_name=trim($uploadfile['name']); //判断文件名是否存在 $sql="select file_name from netdisk_file where file_name='$uploadfile_name' and folder_id=$folder_id"; $allfolder=fetchRow($sql); if($allfolder){ echo "上传的文件不能重名"; }else{ //文件未重名的情况 //保存路径 $uploadfile_save="./uploads/".date('Y-m-d')."/";//保存到/uploads/2018-3-2/里 if(!file_exists($uploadfile_save)){ mkdir($uploadfile_save,0777,true); } $new_uploadfile_name=uniqid().".jpg"; $uploadfile_save.=$new_uploadfile_name; if(move_uploaded_file($uploadfile['tmp_name'],$uploadfile_save)){ //上传成功,并写入数据库 $uploadfile_size=filesize($uploadfile_save); $sql="insert into netdisk_file (file_name,file_save,file_size,file_time,folder_id) values('$new_uploadfile_name','$uploadfile_save',$uploadfile_size,now(),$folder_id)"; if(!mysql_query($sql)){ unlink($uploadfile_save); echo "写入数据库出错"; } } } }else{ //上传失败 } // print_r($uploadfile); }
3, results display:
Next Section