duangang79177 2015-10-12 08:39
浏览 8

如何存储图像并在网站上提供服务

I need a script for a user to upload 5 images for their product on a website. All other users have the ability to view the images only. I think the best way is for the account user to create a folder and then upload required images into it. I dont think I need to register the images into a database because each no searching is required.

If I use the mkdir command and find a script to upload images will all other users get view access only?

Thanks Using php and mysql

  • 写回答

1条回答 默认 最新

  • douji0588 2015-10-12 09:32
    关注

    Since the question is general the answer will have to be general too:

    1. You need webform with POST action
    2. Upload files to the server

    For 1-2 see answer with top votes at How to upload multiple files using PHP, jQuery and AJAX it looks promising (see this link updated PHP code below #3 section)

    1. Save record to DB with images names or link images and DB record some other way What I'd do is 2 tables:

      CREATE TABLE entry ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(64) NOT NULL PRIMARY KEY (id) );

      CREATE TABLE entry_image( id int(11) NOT NULL AUTO_INCREMENT, image_path varchar(255) NOT NULL, PRIMARY KEY (id) );

    3.1 Save 'entry' 3.2 Get it's id 3.3 INSERT all images filenames you have to 'entry_image'

    Here's updated code:

    mysql_query("INSERT INTO entry (name) VALUE('$name')");
    $entryId = mysql_insert_id();
    for($i=0; $i<count($_FILES['file']['name']); $i++){
        $target_path = "uploads/";
        $ext = explode('.', basename( $_FILES['file']['name'][$i]));
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1]; 
    
        if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
            echo "The file has been uploaded successfully <br />";
        mysql_query("INSERT INTO entry_image (entry_id, image_path) VALUES ($entryId, '$target_path');
        } else{
            echo "There was an error uploading the file, please try again! <br />";
        }
    }
    

    Note:

    1. For sure you need to establish mysql connection and close it once done.

    2. Also I'd do all mysql with prepared statements

    3. Data validation (images), error checks, etc

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用