Example of file upload function implemented by Thinkphp5+uploadify
- 2021-10-15 09:55:59
- OfStack
In this paper, the file uploading function realized by Thinkphp5+uploadify is described as an example. Share it for your reference, as follows:
First contact with server-side development, while learning to try to do an OTA background server, it took a lot of effort to achieve file upload and progress bar display.
Encountered several problems:
1. Large file upload failed
2. Upload cancellation X match cannot be displayed
3. Don't know how to pass variable values to background php
Record the process:
1. Download the uploadify code to the project, such as public\ plug-ins\ uploadify.
2. The front-end script is as follows,
The client passes the version number in formData. Please see the version_id assignment method. You need to give it assign in the controller first.
Cancel the match can not be displayed, need to modify uploadify. css inside
background: url('uploadify-cancel.png')
Note the writing of uploader in uploadify
<form enctype="multipart/form-data" method="post" >
<input type="file" name="uploadify" id="uploadify" multiple="true" />
</form>
<script type="text/javascript">
<?php $timestamp = time();?>
var maxSize = 1024 * 1024*1024;//1G
$(function() {
$('#uploadify').uploadify({
'debug' : false,
<span style="white-space:pre"> </span> 'fileSizeLimit ': maxSize,
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>',
<span style="white-space:pre"> </span>'version_id': "{$version_id}"
},
'swf' : '/public/plug-ins/uploadify/uploadify.swf',
<span style="white-space:pre"> </span>'cancelImg':'/public/plug-ins/uploadify/uploadify-cancel.png',
'uploader' : '{:url("Package/upload")}',
<span style="white-space:pre"> </span>'fileTypeDesc' : 'zip Documents ',
<span style="white-space:pre"> </span>'fileTypeExts' : '*.zip',
<span style="white-space:pre"> </span> 'multi': false
});
});
</script>
3. The back-end script corresponds to the upload function of the controller Package
Pay attention to the acquisition method of uploaded files, and cannot use the acquisition method of Thinkphp5 official documents.
Save file name with no special symbol
Modify php. ini:
upload_max_filesize = 1024M
post_max_size=48
Restart service
public function upload(){
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
/*
$targetFolder = '/public/uploads'; // Relative to the root
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','zip'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}*/
$version = model("Version")->retrieve_by_version($_POST['version_id']);
if($version){
$file = new File($tempFile,'rw');
$hash_code = $file->hash();
$time = date("Y-m-d-i-s",$_POST['timestamp']);
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.$version['project_name'].DS.$version['version_name'],'update_'.$time.'.zip');
if($info){
// After successful uploading, Get uploaded information
echo $info->getExtension();
echo $info->getSaveName();
echo $info->getFilename();
}else{
// Upload failed to get error message
echo $file->getError();
}
}else{
echo ' The corresponding version could not be found ';
}
}
}
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.