简体   繁体   English

PHP中的图像上传脚本

[英]image upload script in php

In my application a user can create his/her photo album. 在我的应用程序中,用户可以创建他/她的相册。

My question is how can I upload images specific to that user-id who has uploaded them? 我的问题是如何上传特定于该用户ID的图像? This means there should be some criteria that will indicate that these images belong to this user. 这意味着应该有一些标准来指示这些图像属于该用户。

What I was thinking that I can store images with the image name of the user-id, but in that case it would be difficult to store comments along with these images. 我当时想我可以使用用户ID的图像名称存储图像,但是在那种情况下,很难将注释与这些图像一起存储。

Is there any better way? 有什么更好的办法吗?

A solution that's often used is to have two data for each image : 经常使用的解决方案是每个图像具有两个数据:

  • The file on disk, of course 当然,磁盘上的文件
  • And a record in a database 并在数据库中记录


The record in the database will generally contain at least : 数据库中的记录通常至少包含:

  • The path to the image 图像的路径
  • The Id of the user who uploaded it. 上载用户的ID。

This way, for each image, you have additional data -- and you can use those to add some behavior to your application. 这样,对于每个图像,您就有了其他数据-可以使用这些数据为应用程序添加一些行为。

Of course, it means you'll have a bit more work when a user uploads an image -- but it's not that bad either, and can prove useful. 当然,这意味着当用户上传图像时,您将需要做更多的工作-但这也不错,并且可以证明是有用的。


When needed, too, you'll be able to add some other fields to the database's table storing those data, like, for instance : 同样,在需要时,您也可以向存储这些数据的数据库表中添加一些其他字段,例如:

  • Informations about users who are allowed to see each image 有关被允许查看每个图像的用户的信息
  • Stuff like image size, content-type, last modification time, ... To not have to re-calculate those from the files each time they are needed 诸如图像大小,内容类型,上次修改时间之类的东西。。。不必每次都需要从文件中重新计算它们

Are you not using a database? 您不使用数据库吗?

If no: 如果不:

Set up a database to store the users, then create a logical connection between the iamges and the users. 设置一个数据库来存储用户,然后在iamges和用户之间创建逻辑连接。

for instance 例如

Users: 用户:

|---------------------------------------
|  UserId       Username      Password |
|---------------------------------------
|     1         Robert        3j9745t3 |
|     2          Paul         03945u30 |
|---------------------------------------

Images: 图片:

|--------------------------------------------------
|  ImageID       UserId      Desc       ImageHash |
|-------------------------------------------------|
|     1            1        Some Desc  87ytr8d23yr|
|     2            1        Some Desc  5uty4095u40|
|-------------------------------------------------|

Then store the images on your images directory like 87ytr8d23yr.png and then you can do a php query like so 然后将图像存储在图像目录中,例如87ytr8d23yr.png ,然后可以像这样进行php查询

$userid = 1;
$sql = "SELECT * FROM Images WHERE UserId = " . (int) $userid . " ORDER BY ImageID LIMIT 10";

if(false !== ($resource = mysql_query($sql)))
{
    while($imageRow = mysql_fetch_object($resource))
    {
       if(file_exists('imageDir/' . $imageRow->ImageHash . '.png'))
       {
            //Do whatever.
       }else
       {
           //Delete the row and inform the user that the picture has been deleted for some reason.
       }
    }
}

Also you just have to do a Comments table and add ImageId, and when a user comments on an image just add the image into the comment row so you can trace it. 另外,您只需要做一个Comment表并添加ImageId,当用户对图像进行注释时,只需将图像添加到注释行中即可进行跟踪。

Do you have a database? 你有数据库吗? Are users logged in? 用户登录了吗? Create a table to track images, include a user ID column, and either store the entire image in the database or just the pathname to the image. 创建一个表来跟踪图像,包括一个用户ID列,然后将整个图像存储在数据库中或仅存储图像的路径名。

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

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