-6

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

  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Epodax Oct 12 '15 at 08:40

1 Answers1

0

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

Community
  • 1
  • 1
Vladimir
  • 63
  • 8
  • Thanks, very helpful. Q. why are you saving anything to the db, I thought you could just create a folder and add images to the folder. Cant see why a db is required. When the users browses product then we could just display all images in the fiolder? Pls explain. – Orange Juice Jones Oct 12 '15 at 11:58
  • I'd store images info in DB for flexibility (in future), it doesn't take a lot of space and easier to deal with for the web. If think that's extra then you need to identify product images, probably creating subfolders with product id and save photos there. Your final solution highly depends on project goals, environment, etc – Vladimir Oct 13 '15 at 11:51