首页新闻找找看学习计划

PHP上传图片

0
悬赏园豆:20 [待解决问题]

上传到服务器上,然后把路径保存在数据库中,求教大佬们啊

尅哈地的主页 尅哈地 | 初学一级 | 园豆:164
提问于:2017-11-17 09:32
< >
分享
所有回答(3)
0

百度一抓一打把

子瓜01 | 园豆:235 (菜鸟二级) | 2017-11-17 10:15
0
//判断上传文件是否是图片
        private static bool IsAllowedExtension(FileUpload upfile)
        {
            string strOldFilePath = "";
            string strExtension = "";
            string[] arrExtension = { ".gif", ".jpg", ".bmp", ".png" };
            if (upfile.PostedFile.FileName != string.Empty)
            {
                strOldFilePath = upfile.PostedFile.FileName;//获得文件的完整路径名
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名,如:.jpg
                for (int i = 0; i < arrExtension.Length; i++)
                {
                    if (strExtension.Equals(arrExtension[i]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        //图片上传并将图片重命名
        protected void FileUpload_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                    if (IsAllowedExtension(FileUpload1))
                    {
                        string filepath = FileUpload1.FileName;
                        //string filename = filepath.Substring(filepath.LastIndexOf('\\') + 1, filepath.Length - filepath.LastIndexOf('\\') - 1);//(filepath.LastIndexOf("\\") + 1);
                        //以年-月-日-时-分-秒-毫米将图片重新命名
                          string filename = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fffffff")+filepath.Substring(filepath.LastIndexOf('.'), filepath.Length - filepath.LastIndexOf('.'));
                        //设定上传路径(绝对路径)
                          string uppath = Server.MapPath("~/Photo/") + filename;
                        //图片上传至绝对路径
                          FileUpload1.PostedFile.SaveAs(uppath);
                        //设定数据库的存储路径
                          string savepath="~\\Photo\\"+filename;
                        HiddenField1.Value = savepath;
                        lblInfo.Text = "上传成功!";
 
                    }
                }
                else
                {
                    lblInfo.Text = "请选择上传文件";
                }
            }
            catch (Exception ex)
            {
                lblInfo.Text = "上传发生错误!原因是:" + ex.ToString();
            }
        }
 数据库操作应该会吧
冯冯瑞瑞 | 园豆:221 (菜鸟二级) | 2017-11-17 10:18

哇,这个...布局,看着头皮发麻.

支持(0) 反对(0) 长蘑菇星人 | 园豆:1820 (小虾三级) | 2017-11-17 14:11
0

看一下基本的上传知识就可以解决你这个问题了

YQZC | 园豆:2354 (老鸟四级) | 2017-11-23 11:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册