i think i am missing something in the below script:
It shows error an do not upload files to destination: Let me know how to solve this: - <?php
-
-
if (isset($_POST[submit]))
-
{
-
$uploadArray= array();
-
$uploadArray[] = $_POST['uploadedfile'];
-
$uploadArray[] = $_POST['uploadedfile2'];
-
$uploadArray[] = $_POST['uploadedfile3'];
-
-
foreach($uploadArray as $file)
-
{
-
$target_path = "upload/";
-
$target_path = $target_path . basename( $_FILES["$file"]['name']);
-
-
if(move_uploaded_file($_FILES["$file"]['tmp_name'], $target_path))
-
{
-
echo "The file ". basename( $_FILES["$file"]['name'])." has been uploaded";
-
}
-
else
-
{
-
echo "There was an error uploading the file, please try again!";
-
}
-
}
-
}
-
?>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
-
<title>Untitled Document</title>
-
</head>
-
-
<body>
-
<form enctype="multipart/form-data" action="" method="POST">
-
<p>
-
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
-
Choose a file to upload:
-
<input name="uploadedfile" type="file" />
-
</p>
-
<p>Choose a file to upload:
-
<input name="uploadedfile2" type="file" />
-
</p>
-
<p>Choose a file to upload:
-
<input name="uploadedfile3" type="file" />
-
<br />
-
<input name="submit" type="submit" id="submit" value="submit" />
-
</p>
-
</form>
-
</body>
-
</html>
-
Error Msg: - Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
-
There was an error uploading the file, please try again!
-
Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 22
-
-
Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
-
There was an error uploading the file, please try again!
-
Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 22
-
-
Notice: Undefined index: in E:\wamp\www\fileupload\index.php on line 25
-
There was an error uploading the file, please try again!
I think that Bytes.com's [code] tags are glitchy... Your code has seemingly random HTML entities scattered about and, in "Expand" mode, the ampersands are all converted to "&". So, I'll try to ignore the HTML entities when responding. It's also be nice if Bytes.com had syntax highlighting. *ahem* Anyway...
Basically, your main problem seems to be this undefined index. Your error message has no entry for the undefined index, as you can see. This is because it is looking for index "". That's an empty string, and likely not what you were going for. On lines 22 and 25, you are attempting to look at $_FILES["$file"]. $file is defined in your foreach loop as an element of $uploadArray. However, your $uploadArray array consists of $_POST data which is, firstly, nonexistent for your upload form and, secondly, wouldn't be a scalar value (i.e. string, integer) if you were using the correct array, $_FILES, anyway. Therefore, it results in an empty value.
I have two suggestions for you: 1. Make use of form arrays in your form. - <form action="#" method="post" enctype="multipart/form-data">
-
<input type="file" name="files[]" />
-
<input type="file" name="files[]" />
-
<input type="file" name="files[]" />
-
<button type="submit">Upload</button>
-
</form>
When this is submitted, $_FILES['files'] will be an array of 3 uploaded files, in the order that they exist in the form. This also provides you with the flexibility of having a variable amount of uploaded files, and grants you the ability to add more input fields dynamically (i.e. via JavaScript) without changing any of the back-end code. 2. Traverse the form array for uploading. - if (!empty($_FILES)) { // Form has been submitted
-
if (!empty($_FILES['files']) && is_array($_FILES['files'])) { // The file array is formatted correctly
-
for ($i = 0; $i < sizeof($_FILES['files']['name']); $i++) {
-
// Perform upload with $_FILES['files'][<attribute>][$i]
-
}
-
}
-
}
You cannot use a foreach loop when you upload multiple files together in a form array, sadly, because PHP organizes them differently. You'd think that each element of the $_FILES['files'] array would be an individual input field from the form, but instead PHP forces it to conform with the "$_FILES[<name>][<attribute>]" format. This is done for safety reasons, though I personally feel we should at least have an option. Not that my personal opinion matters. :P
Anyway, what this means is that, for example, in order to get the temporary file name of the first uploaded file, you would look at $_FILES['files']['tmp_name'][0]. To get the temporary file name of the second uploaded file, you look at $_FILES['files']['tmp_name'][1]. And so on. This is why we need a for loop, instead of a foreach loop, so that we can use $i as the index to check.
4 4082
I think that Bytes.com's [code] tags are glitchy... Your code has seemingly random HTML entities scattered about and, in "Expand" mode, the ampersands are all converted to "&". So, I'll try to ignore the HTML entities when responding. It's also be nice if Bytes.com had syntax highlighting. *ahem* Anyway...
Basically, your main problem seems to be this undefined index. Your error message has no entry for the undefined index, as you can see. This is because it is looking for index "". That's an empty string, and likely not what you were going for. On lines 22 and 25, you are attempting to look at $_FILES["$file"]. $file is defined in your foreach loop as an element of $uploadArray. However, your $uploadArray array consists of $_POST data which is, firstly, nonexistent for your upload form and, secondly, wouldn't be a scalar value (i.e. string, integer) if you were using the correct array, $_FILES, anyway. Therefore, it results in an empty value.
I have two suggestions for you: 1. Make use of form arrays in your form. - <form action="#" method="post" enctype="multipart/form-data">
-
<input type="file" name="files[]" />
-
<input type="file" name="files[]" />
-
<input type="file" name="files[]" />
-
<button type="submit">Upload</button>
-
</form>
When this is submitted, $_FILES['files'] will be an array of 3 uploaded files, in the order that they exist in the form. This also provides you with the flexibility of having a variable amount of uploaded files, and grants you the ability to add more input fields dynamically (i.e. via JavaScript) without changing any of the back-end code. 2. Traverse the form array for uploading. - if (!empty($_FILES)) { // Form has been submitted
-
if (!empty($_FILES['files']) && is_array($_FILES['files'])) { // The file array is formatted correctly
-
for ($i = 0; $i < sizeof($_FILES['files']['name']); $i++) {
-
// Perform upload with $_FILES['files'][<attribute>][$i]
-
}
-
}
-
}
You cannot use a foreach loop when you upload multiple files together in a form array, sadly, because PHP organizes them differently. You'd think that each element of the $_FILES['files'] array would be an individual input field from the form, but instead PHP forces it to conform with the "$_FILES[<name>][<attribute>]" format. This is done for safety reasons, though I personally feel we should at least have an option. Not that my personal opinion matters. :P
Anyway, what this means is that, for example, in order to get the temporary file name of the first uploaded file, you would look at $_FILES['files']['tmp_name'][0]. To get the temporary file name of the second uploaded file, you look at $_FILES['files']['tmp_name'][1]. And so on. This is why we need a for loop, instead of a foreach loop, so that we can use $i as the index to check.
Hey thanks for your reply,
I have achived this in folowing way: -
-
$image_count = count($_FILES[image][name]);
-
//db($_FILES);
-
for($i = 0; $i < $image_count; $i++)
-
{
-
-
$src = $_FILES['image']['tmp_name'][$i];
-
-
$destination = BASE_DIR . 'upload/' .$_FILES['image']['name'][$i];
-
if (move_uploaded_file($src, $destination))
-
{
-
chmod($destination, 0664);
-
$detail[temp_image]=$_FILES['image']['name'];
-
}
-
else
-
{
-
die('Sorry, the system was unable to upload the project image.');
-
}
-
}
-
Hey thanks for your reply,
However, I have achieved this in following way, -
-
$image_count = count($_FILES[image][name]);
-
//db($_FILES);
-
for($i = 0; $i < $image_count; $i++)
-
{
-
-
$src = $_FILES['image']['tmp_name'][$i];
-
-
$destination = BASE_DIR . 'upload/' .$_FILES['image']['name'][$i];
-
if (move_uploaded_file($src, $destination))
-
{
-
chmod($destination, 0664);
-
$detail[temp_image]=$_FILES['image']['name'];
-
}
-
else
-
{
-
die('Sorry, the system was unable to upload the project image.');
-
}
-
}
-
-
$image=$detail[temp_image];
-
-
Thanks again for your response.
Why do you reassign $detail['temp_image'] every time a file is successfully uploaded?
Also, don't forget to wrap your array keys in quotation marks. When you don't, then PHP assumes that you meant a constant. It wastes time looking for that constant before determining that you meant a string. And, if it does find a constant, it will overwrite your string.
Like this:
$detail[temp_image] should be $detail['temp_image']
$_FILE[image][name] should be $_FILES['image']['name']
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dave |
last post by:
Hello,
I'm trying to get the below script working, adapted from the manual on
php.net. Although i am getting success file uploading, i do not see the file
in the tempoary area. And i'm not sure...
|
by: Avin Patel |
last post by:
Hi,
I am looking for script to allow multiple files can be
uploaded/attached in webform ( mostly cgi/perl or php).
But I don't like the multiple input boxes using "<input type="file"
size="40"...
|
by: Shawn H. Mesiatowsky |
last post by:
I am creating an intranet App that is a document management system, and now
I have been told they wan't the ebilty to version control directories, not
just files. So I have the file upload and...
|
by: Yandos |
last post by:
Hello all,
I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might
help me here...
I need to feed form like the following using libcurl:
<form...
|
by: Atli |
last post by:
Hi.
I'm trying to use php code I wrote last year when I was using Apache, so I'm pretty sure the code works.
Its a simple code to upload an image and create a thumb of the image in another...
|
by: =?Utf-8?B?U2NvdHQ=?= |
last post by:
I have a page with four <input type=filecontrols on it. When i upload less
than four meg across all four inputs, the files upload correctly. When i
upload more than four meg, i get an error message...
|
by: der_grobi |
last post by:
That is the Problem:
I have an ASP.NET Webapplicatipon where I can upload single files to
the Server.
That works fine. But now, I want to Upload multiple files. I know the
path of the files, i...
|
by: John Devlon |
last post by:
Hi,
Some people like to go on vacation during christmas time, others try to do
something they never did before....
I would like to create a multiple file upload page, with some nice progress...
|
by: torso |
last post by:
Hi
Does someone know a good tutorial for multiple file upload with
xmlHttpRequest. I am trying to do directory upload. So I could choose
directorys and upload those to the server.
Another...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |