Reputation: 1337
using the following PHP I am attempting to upload multiple images. The number of images upload can be varied.
The problem that I seem to have is that image number 1 isn't being uploaded however it's filepath is being printed to the screen.
The code:-
if ($_FILES['pac_img_1']['name']>""){
echo("You have uploaded the following images:-<ul>");
for ($i=1; $i<=$imagesCount; $i++){
$target_path = "files/" . $companyName . "/images/";
$target_path = $target_path . basename( $_FILES['pac_img_' . $i]['name']);
if(move_uploaded_file($_FILES['pac_img_' . $i]['tmp_name'], $target_path)) {
echo "<li><a href='". $target_path . "'>". basename( $_FILES['pac_img_' . $i]['name']). "</a></li>";
} else{
echo "There was an error uploading an image";
}
};
echo("</ul>");
}else{
echo("None uploaded");
};
I have adapted it from some code that I've used before so I suspect I am guilty of a "schoolboy" error here.
Help would be appreciated.
Edit to add that $imagesCount takes its value from a form element via a $_POST request. When only one image is uploaded that value = 0.
Upvotes: 1
Views: 533
Reputation: 908
Your for loop need to be modified. Array indexes starts from 0. And the last element should be Array length - 1; Your for loop need to be modified as bellow code sample.
Actually, it's looping through several $_POST itens. His HTML probably has something like:
<input type="file" name="pac_img_1">
<input type="file" name="pac_img_2">
<input type="file" name="pac_img_3">
and he is trying to get these images.
I would do this differently.
HTML:
<input type="file" name="pac_img[]" />
<input type="file" name="pac_img[]" />
<input type="file" name="pac_img[]" />
(note that you can dynamically add file inputs without worrying about names)
PHP:
if (count($_FILES['pac_img']) > 0){
echo("You have uploaded the following images:-<ul>");
foreach($_FILES['pac_img'] as $key => $file){
$target_path = "files/" . $companyName . "/images/";
$target_path = $target_path . basename( $file['name']);
if(move_uploaded_file($file['tmp_name'], $target_path)) {
echo "<li><a href='". $target_path . "'>". basename( $file['name'] ). "</a></li>";
} else{
echo "There was an error uploading an image";
}
}
echo("</ul>");
}else{
echo("None uploaded");
}
And last, but not least: ALWAYS check if uploaded files are what they suposed to be. (http://www.acunetix.com/websitesecurity/upload-forms-threat.htm)
Upvotes: 0
Reputation: 6180
Your for loop need to be modified. Array indexes starts from 0. And the last element should be Array length - 1;
Your for loop need to be modified as bellow code sample.
if ($_FILES['pac_img_1']['name']>""){
echo("You have uploaded the following images:-<ul>");
for ($i=0; $i<$imagesCount; $i++){
$target_path = "files/" . $companyName . "/images/";
$target_path = $target_path . basename( $_FILES['pac_img_' . $i]['name']);
if(move_uploaded_file($_FILES['pac_img_' . $i]['tmp_name'], $target_path)) {
echo "<li><a href='". $target_path . "'>". basename( $_FILES['pac_img_' . $i]['name']). "</a></li>";
} else{
echo "There was an error uploading an image";
}
};
echo("</ul>");
}else{
echo("None uploaded");
};
Upvotes: 0
Reputation: 578
Without being a php-dude at all, I'd try changing
for ($i=1; $i<=$imagesCount; $i++){
to
for ($i=0; $i<=$imagesCount; $i++){
-or maybe
for ($i=0; $i < $imagesCount; $i++){
depending on how $imagesCount is set.
Upvotes: 1