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

    评论

报告相同问题?