简体   繁体   English

MySQL图像上传

[英]mySQL images upload

I need a simple code to upload images to mySQL using PHP... short! 我需要一个简单的代码即可使用PHP将图像上传到mySQL。 snippet... and is it possible to upload an html, css file to mySQL?... its reason is complicated but all answers are appreciated!... EDIT:: say I have 1000 users.. and they each have their own layout for their page.. So inside their MYSQL record will be a html file, css file(possibly), and image(s)... 片段...并且可以将html,css文件上传到mySQL吗?...其原因很复杂,但是所有答案都值得赞赏!...编辑::说我有1000个用户..他们每个人都有自己的页面的布局。.因此,在他们的MYSQL记录中将是html文件,css文件(可能)和图像...

I am a big fan of using a filesystem for storing physical files, i've yet to see any solid reason why they are better off in a database. 我是使用文件系统存储物理文件的忠实拥护者,但至今还没有任何可靠的理由可以使它们在数据库中得到更好的利用。

To automate this process you could have a shell script called through exec 要自动执行此过程,您可以通过exec调用一个shell脚本

exec("/home/some/path/my_filesystem_creator.sh ".escapeshellarg($args));

or PHP's native mkdir or anything really. 或PHP的本地mkdir或其他任何东西。 If you went for a structure like: 如果您采用的结构是:

/common/
/userdirs/1/
/userdirs/2/

essentially all i would imagine you would need to do is create a user dir, and copy into it the default versions of their site assets - images/css/html etc. 基本上,我想您需要做的就是创建用户目录,并将其站点资产的默认版本复制到该目录中-images / css / html等。

This should be easy enough to manage 这应该很容易管理

Are you asking how to store a file in the database? 您是否在询问如何在数据库中存储文件?

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx

Or do you need to know how to upload a file to your web server in order to display it in a PHP/MySQL website? 还是您需要知道如何将文件上传到Web服务器以便在PHP / MySQL网站中显示它?

Your page would be faster, if you generate a directory on your filespace for each user and store their css/js/image files there. 如果您在文件空间上为每个用户生成一个目录并在其中存储其css / js / image文件,则页面会更快。

The reason for this is, that when you like to output your images to the browser, you will need to establish an own db connection for each file (since each is an own HTTP request to a PHP file, selecting the image). 这样做的原因是,当您想将图像输出到浏览器时,需要为每个文件建立一个自己的数据库连接(因为每个都是对PHP文件的自己的HTTP请求,因此选择该图像)。

You might want to take a look at http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.html and http://hashmysql.org/index.php?title=Storing_files_in_the_database before doing that. 您可能想先看看http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.htmlhttp://hashmysql.org/index.php?title=Storing_files_in_the_database那样做。 Storing files in mysql is generally considered a bad idea. 在mysql中存储文件通常被认为是一个坏主意。

Just use different CSS rules for each user. 只需为每个用户使用不同的CSS规则。 Create the CSS dynamically though PHP based on user-specific variables. 通过基于用户特定变量的PHP动态创建CSS。 For example, if they have a div with an avatar or some other personal image, just create a class that uses variables for images, and then you really only need one or two files at most to do the whole thing. 例如,如果他们有一个带化身的div或其他个人图像,则只需创建一个使用图像变量的类,然后您实际上最多只需要一个或两个文件即可完成整个操作。 I would use a heredoc, but you could just use quotation marks to integrate the PHP. 我将使用heredoc,但是您可以使用引号将PHP集成在一起。

php creates your css - PHP创建您的CSS-

.useravatar{ 'background: url($baseurl.$urseridpic)'} 

In the html, the div just needs the class of 'useravatar' never needing to be changed. 在html中,div仅需要“ useravatar”类,无需更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM