Solved

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

Posted on 2003-11-12
4
414 Views
Last Modified: 2011-09-20
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
Comment
Question by:abulink
4 Comments
 

Author Comment

by:abulink
ID: 9736835
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
 
LVL 13

Accepted Solution

by:
lozloz earned 250 total points
ID: 9737389
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now