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
413 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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

7 Experts available now in Live!

Get 1:1 Help Now