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
Since the question is general the answer will have to be general too:
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)
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:
For sure you need to establish mysql connection and close it once done.
Also I'd do all mysql with prepared statements
Data validation (images), error checks, etc