Laravel5.2 integrates Uploadify to implement code examples for uploading images

黄舟
Release: 2023-03-15 21:48:01
Original
1366 people have browsed it

Front-end:


<!-- 引入CSS、JS -->
<link rel="stylesheet" type="text/css" href="{{asset(&#39;org/uploadify/uploadify.css&#39;)}}">
<script type="text/javascript" src="{{asset(&#39;admin/js/jquery.js&#39;)}}"></script>
<script src="{{asset(&#39;org/uploadify/jquery.uploadify.min.js&#39;)}}" type="text/javascript"></script>

<!-- 上传按钮 -->
<input id="file_upload" name="file_upload" type="file"><br>
<img id="picshow" src=""> <!-- 默认隐藏 #pic{display: none;} -->

<!-- 实例化 -->
<script>
    $(&#39;#file_upload&#39;).uploadify({
            swf      : "{{asset(&#39;org/uploadify/uploadify.swf&#39;)}}", // 引入Uploadify 的核心Flash文件
            uploader : "{{url&#39;admin/upload&#39;)}}", // PHP脚本地址
            width: 120, // 上传按钮宽度
            height: 30, // 上传按钮高度
            buttonImage: "{{asset(&#39;org/uploadify/browse-btn.png&#39;)}}", // 上传按钮背景图片地址
            fileTypeDesc: &#39;Image File&#39;, // 选择文件对话框中图片类型提示文字(Windows系统)
            fileTypeExts: &#39;*.jpg;*.jpeg;*.png;*.gif&#39;, // 选择文件对话框中允许选择的文件类型(Windows系统)
            formData     : {&#39;_token&#39;: &#39;{{csrf_token()}}&#39;}, // Laravel表单提交必需参数_token,防止CSRF
            onUploadSuccess : function(file, data, response) { // 上传成功回调函数
                $(&#39;#picshow&#39;).attr(&#39;src&#39;, data).show();
                $(&#39;#file_upload).val(data);
            },
            onUploadError: function(file, errorCode, errorMsg, errorString) { // 上传失败回调函数
                $(&#39;#picshow&#39;).attr(&#39;src&#39;, &#39;&#39;).hide();
                $(&#39;#file_upload).val(&#39;&#39;);
                alert(&#39;上传失败,请重试!&#39;);
            }
        });
</script>
Copy after login

Back-end:


/**
 * 图片上传
 * @return [type] [description]
 */
public function upload()
{
    $file = Input::file(&#39;Filedata&#39;); // 不同环境可能获取方式有点不同,可以下打印观察一下 dd(Input());
    if($file->isValid())
    {
        // 上传目录。 public目录下 uploads/thumb 文件夹
        $dir = &#39;uploads/thumb/&#39;;
 
        // 文件名。格式:时间戳 + 6位随机数 + 后缀名
        $filename = time() . mt_rand(100000, 999999) . &#39;.&#39; . $file ->getClientOriginalExtension();
         
        $file->move($dir, $filename);
        $path = $dir . $filename;
        return url($path);
    }
}

// $realPath = $file->getRealPath();  // 缓存在 tmp 文件夹的文件绝对路径
// $tmpName = $file->getFileName();   // 缓存在 tmp 文件夹的文件名
// $clientName = $file->getClientOriginalName();     // 获取原文件名称
// $extension = $file->getClientOriginalExtension(); // 上传文件的后缀
Copy after login

The above is the detailed content of Laravel5.2 integrates Uploadify to implement code examples for uploading images. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!