I'm writing a script that uploads multiple files, but nothing's happening. Here's the code:
addlisting.php
<html>
<head>
<title>Add Listing</title>
</head>
<body>
<form action="../submit_addlisting.php" method="post" onSubmit="this.ref.value = document.referrer;" enctype="multipart/form-data">
Files to Upload:<br>
<input type="file" name="image1" size="30"><br>
<input type="file" name="image2" size="30"><br>
<input type="file" name="image3" size="30"><br>
<input type="file" name="image4" size="30"><br>
<input type="file" name="image5" size="30"><br>
<input type="submit" value="Submit" id="submit1">
</form>
</body>
</html>
submit_addlisting.php
<?php
$a = 1;
$dir = $_ENV['DOCUMENT_ROOT'] . "/www/";
while ($a <= "5") {
$img = $_FILES['$image[$a]'];
$img_name = $_FILES['$image[$a]']['name'];
$img_tmpname = $_FILES['image[$a]']['tmp_name'];
if ($img != "") {
@copy($img_tmpname, $dir . $img_name) or die("Couldn't copy the file.");
echo "$img_name was uploaded succesfully.<br>";
$a++;
} else {
$a++;
}
}
?>you're referencing your images wrong in the $_FILES array.
the way you've named them, you can do:
$_FILES['image'. $a]
better yet would be to write the input names in the form as:
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
then in the form handler:
foreach($_FILES as $f)
{
@move_uploaded_file($f['tmp_name'], $dir . $f['name']) or die("Couldn't upload the file.");
}
1. this way - the script will ignore empty entries.
2. using move_uploaded_file means that the temp file is deleted after it is placed where you want it.
3. it is easily expandable - just add more file inputs in the form.I like your code a little better cause it's a cleaner. Now if they only specify 3/5 files, how can I show that those three were successfully uploaded? Here's what I tried, but it didn't work:
<?php
foreach($_FILES as $f) {
if ($f != "") {
$file = $f['name'];
@move_uploaded_file($f['tmp_name'], $dir . $f['name']);
echo "<b>$file</b> was uploaded successfully.<br>";
} else {
echo "No file specified.";
}
}
?>nevermind, i figured it out...
<?php
foreach($_FILES as $f) {
if ($f['name'] != "") {
@move_uploaded_file($f['tmp_name'], $dir . $f['name']) or die("Couldn't upload the file.");
echo "<b>" . $f['name'] . "</b>" . " was uploaded successfully.<br>";
}
}
?>I would do it this way. actually I don't think the way you have would work. you have an array in teh form so it creates anotehr part of the array and you miss that in the foreach loop. well it might but a lot more difficult.
<?php
$num_files = count($_FILES["image"]["tmp_name"]);
for($i=0; $i<$num_files; $i++){
if (is_uploaded_file($_FILES['image']['tmp_name'][$i])){
@move_uploaded_file($_FILES['image']['tmp_name'][$i], $dir . $_FILES['image']['name'][$i]) or die("Couldn't upload the file.");
echo "<b>" . $_FILES['image']['name'][$i] . "</b>" . " was uploaded successfully.<br>";
}
}
?>d'oh - yeah, sorry....
should have done:
foreach($_FILES['image'] as $f)
was late, okay...Now i'm trying to display each of the images...
$num_files = count($_FILES["image"]["tmp_name"]);
for($i = 0; i < $num_files; $i++) {
if ($_FILES['image']['name'][$i] != "") {
echo "<img border=\"1\" src=http://www.htmlforums.com/archive/index.php/\"" . $_FILES['image']['name'][$i] . "\" width=\"66\" height=\"50\">";
}
}
But this isn't working. I'm not sure if it has to do with thte fact that the files have been moved or what. Any ideas?well yeah, after they get moved you have to call them normally. you can't use $_FILES anymore. well you could do that but you have to give them the url to where they are at.and this is what I get
an error has occurred (-32768).
FTP error. The server could not complete the transfer; try again later.
can someone help me figure this out.
im using this server side on an FTP server.this code won't give that kind of error. you must be doing somethign else. you say a ftp server? then tha tis why, you can't do it on a ftp server. use the ftp functions in php instead.What are the ftp functions in PHP, do you have the code handy.
this is the HTML im using'
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Mac">
<TITLE>Add Listing</TITLE>
</HEAD>
<BODY>
<FORM ACTION="submit_addlisting.php" METHOD="POST" onSubmit="this.ref.value = document.referrer;"
ENCTYPE="multipart/form-data">
<!--SELECTION--><!--/SELECTION-->Files to Upload:<BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<INPUT TYPE="submit" VALUE="Submit" ID="submit1"></FORM>
</BODY>
</HTML>ok, now I am lost. why are you using html in a ftp server?
now show us the code you have in submit_addlisting.phpPHP code
<?php
$num_files = count($_FILES["image"]["tmp_name"]);
for($i=0; $i<$num_files; $i++){
____if (is_uploaded_file($_FILES['image']['tmp_name'][$i])){
________@move_uploaded_file($_FILES['image']['tmp_name'][$i], $dir . $_FILES['image']['name'][$i]) or die("Couldn't upload the file.");
_________echo "<b>" . $_FILES['image']['name'][$i] . "</b>" . " was uploaded successfully.<br>";
_____}
}
?>
What would you recommend, I work for a print house which operates a FTP site for customers to use, some customers have trouble accessing via and FTP client so im trying to put together a server side utility for them to access and upload PDF files via a web browser, the FTP server is local.
Thanksyou have the right idea but I don't think you can do it on a ftp site (notice the error) it won't allow POST's from a form, you have to use a http side of things.
addlisting.php
<html>
<head>
<title>Add Listing</title>
</head>
<body>
<form action="../submit_addlisting.php" method="post" onSubmit="this.ref.value = document.referrer;" enctype="multipart/form-data">
Files to Upload:<br>
<input type="file" name="image1" size="30"><br>
<input type="file" name="image2" size="30"><br>
<input type="file" name="image3" size="30"><br>
<input type="file" name="image4" size="30"><br>
<input type="file" name="image5" size="30"><br>
<input type="submit" value="Submit" id="submit1">
</form>
</body>
</html>
submit_addlisting.php
<?php
$a = 1;
$dir = $_ENV['DOCUMENT_ROOT'] . "/www/";
while ($a <= "5") {
$img = $_FILES['$image[$a]'];
$img_name = $_FILES['$image[$a]']['name'];
$img_tmpname = $_FILES['image[$a]']['tmp_name'];
if ($img != "") {
@copy($img_tmpname, $dir . $img_name) or die("Couldn't copy the file.");
echo "$img_name was uploaded succesfully.<br>";
$a++;
} else {
$a++;
}
}
?>you're referencing your images wrong in the $_FILES array.
the way you've named them, you can do:
$_FILES['image'. $a]
better yet would be to write the input names in the form as:
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
<input type="file" name="image[]" size="30"><br>
then in the form handler:
foreach($_FILES as $f)
{
@move_uploaded_file($f['tmp_name'], $dir . $f['name']) or die("Couldn't upload the file.");
}
1. this way - the script will ignore empty entries.
2. using move_uploaded_file means that the temp file is deleted after it is placed where you want it.
3. it is easily expandable - just add more file inputs in the form.I like your code a little better cause it's a cleaner. Now if they only specify 3/5 files, how can I show that those three were successfully uploaded? Here's what I tried, but it didn't work:
<?php
foreach($_FILES as $f) {
if ($f != "") {
$file = $f['name'];
@move_uploaded_file($f['tmp_name'], $dir . $f['name']);
echo "<b>$file</b> was uploaded successfully.<br>";
} else {
echo "No file specified.";
}
}
?>nevermind, i figured it out...
<?php
foreach($_FILES as $f) {
if ($f['name'] != "") {
@move_uploaded_file($f['tmp_name'], $dir . $f['name']) or die("Couldn't upload the file.");
echo "<b>" . $f['name'] . "</b>" . " was uploaded successfully.<br>";
}
}
?>I would do it this way. actually I don't think the way you have would work. you have an array in teh form so it creates anotehr part of the array and you miss that in the foreach loop. well it might but a lot more difficult.
<?php
$num_files = count($_FILES["image"]["tmp_name"]);
for($i=0; $i<$num_files; $i++){
if (is_uploaded_file($_FILES['image']['tmp_name'][$i])){
@move_uploaded_file($_FILES['image']['tmp_name'][$i], $dir . $_FILES['image']['name'][$i]) or die("Couldn't upload the file.");
echo "<b>" . $_FILES['image']['name'][$i] . "</b>" . " was uploaded successfully.<br>";
}
}
?>d'oh - yeah, sorry....
should have done:
foreach($_FILES['image'] as $f)
was late, okay...Now i'm trying to display each of the images...
$num_files = count($_FILES["image"]["tmp_name"]);
for($i = 0; i < $num_files; $i++) {
if ($_FILES['image']['name'][$i] != "") {
echo "<img border=\"1\" src=http://www.htmlforums.com/archive/index.php/\"" . $_FILES['image']['name'][$i] . "\" width=\"66\" height=\"50\">";
}
}
But this isn't working. I'm not sure if it has to do with thte fact that the files have been moved or what. Any ideas?well yeah, after they get moved you have to call them normally. you can't use $_FILES anymore. well you could do that but you have to give them the url to where they are at.and this is what I get
an error has occurred (-32768).
FTP error. The server could not complete the transfer; try again later.
can someone help me figure this out.
im using this server side on an FTP server.this code won't give that kind of error. you must be doing somethign else. you say a ftp server? then tha tis why, you can't do it on a ftp server. use the ftp functions in php instead.What are the ftp functions in PHP, do you have the code handy.
this is the HTML im using'
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Mac">
<TITLE>Add Listing</TITLE>
</HEAD>
<BODY>
<FORM ACTION="submit_addlisting.php" METHOD="POST" onSubmit="this.ref.value = document.referrer;"
ENCTYPE="multipart/form-data">
<!--SELECTION--><!--/SELECTION-->Files to Upload:<BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<input type="file" name="image[]" size="30"><BR>
<INPUT TYPE="submit" VALUE="Submit" ID="submit1"></FORM>
</BODY>
</HTML>ok, now I am lost. why are you using html in a ftp server?
now show us the code you have in submit_addlisting.phpPHP code
<?php
$num_files = count($_FILES["image"]["tmp_name"]);
for($i=0; $i<$num_files; $i++){
____if (is_uploaded_file($_FILES['image']['tmp_name'][$i])){
________@move_uploaded_file($_FILES['image']['tmp_name'][$i], $dir . $_FILES['image']['name'][$i]) or die("Couldn't upload the file.");
_________echo "<b>" . $_FILES['image']['name'][$i] . "</b>" . " was uploaded successfully.<br>";
_____}
}
?>
What would you recommend, I work for a print house which operates a FTP site for customers to use, some customers have trouble accessing via and FTP client so im trying to put together a server side utility for them to access and upload PDF files via a web browser, the FTP server is local.
Thanksyou have the right idea but I don't think you can do it on a ftp site (notice the error) it won't allow POST's from a form, you have to use a http side of things.