[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

Trying to build a image upload script. by browsing for file and sending to listmenu. then uploading as arry from listmenu.

Hi all . what im trying to do is have a button on my page that you click it opens a file browser window. you select a file (image) hit the ok button the window closes and inserts the image into a multilined list menu. then you can select another image and add it to the list menu.

then once you have finished you hit the another button on the page (upload)  and it upload the listmenu as an array to the upload script. the upload scripts reads how many files there are in the array and uploads the images to the webserver and inserts the image name into the database.

i know this is asking alot but i havent got a clue how to start it.
any help is appreciated.
0
abulink
Asked:
abulink
1 Solution
 
abulinkAuthor Commented:
i want to do what this does but not in java. i want to use php and javascript.

http://www.haller-systemservice.net/jupload/demo/
0
 
lozlozCommented:
hi,

here's how you would do it:

at the very top of the page, you need session_start();. for your form, you need just a file upload box (input type="file" name="fileupload"), and have the form with action="<? print $_SERVER["PHP_SELF"]; ?>". above this html, you'll want to do the processing. to detect a file being uploaded, you can use if($_FILES["file"]["name"]) {. then you need to copy the file to your images folder, so copy($_FILES["file"]["tmp_name"], "../images/" . $_FILES["file"]["name"]); or something. after this you should add the name of the file to an array of files in your session. $_SESSION["uploaded"][] = $_FILES["file"]["name"];. if you wanted to display the images currently uploaded, you could put <ul> around a foreach($_SESSION["uploaded"] as $val) { clause and have print "<li><img src=\"/images/$val\" alt=\"$val\"></li>\n"; inside it. your form should have two buttons, one being upload and one being finish.. so above your form, you need an elseif clause for the the if($_FILES["file"]["name"] { part.. this will deal with the files listed in $_SESSION["uploaded"] by adding entries to the db. within this clause you would have foreach($_SESSION["uploaded"] as $key => $value) {
  $query = "INSERT INTO table (name) VALUES ('" . $value . "')";
  $result = mysql_query($query) or die(mysql_error() . $query);
  $_SESSION["uploaded"][$key] = "";
}

that's probably really hard to follow.. here's a break down of how the script should look

<?
session_start();
if($_FILES["file"]["name"]) {
  # use copy() to upload the image to the correct directory
  # store the name in the session array
} elseif($_POST["checkout"]) {
  # loop through each item in the session array variable you have for uploaded files and execute a query to add them to the database
  # unset the appropriate session variable so it won't be added again
}
# print list of currently uploaded images if you want
print "<ul>\n";
foreach($_SESSION["uploaded"] as $val) {
  print "<li><img src=\"/images/$val\" alt=\"$val\"></li>\n";
}
print "</ul>\n<br />";  
# output your form
?>
<form action="<? print $_SERVER["PHP_SELF"]; ?>" method="POST">
File: <input type="file" name="fileupload" size="40"><br /><br />
Add file: <input type="submit" name="addfile" value="Add file"> | Complete uploads: <input type="submit" name="checkout" value="Checkout">
</form>

hope it helps

loz
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now