0

I want to upload image to webserver. When I upload one image, variable i will be increased 1. But now the variable i can't be increased. I want to keep uploading many images. What's wrong with my php? Here is my php:

<?php
$i = 0;
$file = "image".$i.".png";
if (move_uploaded_file($_FILES['file']['tmp_name'], "$file" )) 
{
$i++;
echo "File uploaded: ".$_FILES["file"]["name"]; 

}
?>
HappyChan
  • 17
  • 1
  • 6
  • Your question is vage, but it _might_ be that you wonder why `$i` always starts at `0` again for each uploaded file? The simple answer to that is: each file you upload is a separate request which results in a separate script run. You need some form of persistent storage instead, some database for example where you keep track of the numbers you already used. – arkascha Apr 21 '17 at 09:32
  • @Brijesh_yadav Yes I can upload one image, but can't upload more – HappyChan Apr 21 '17 at 09:32
  • It is far easier to use a timestamp instead of an incremented number if you do _not_ want to use a persistent storage for counting. – arkascha Apr 21 '17 at 09:33
  • Possible duplicate of [Multiple image upload](http://stackoverflow.com/questions/35960239/multiple-image-upload) – AddcitedToLearn Apr 21 '17 at 09:34
  • @arkascha I can't use timestamp because I need to receive the image from the server. – HappyChan Apr 21 '17 at 09:35
  • Start by [Reading The Manual](http://php.net/manual/en/features.file-upload.multiple.php) its all in there somewehere – RiggsFolly Apr 21 '17 at 09:36
  • To upload more than one file you need to LOOP over the $_FILES array – RiggsFolly Apr 21 '17 at 09:38
  • Sorry, I can't make much sense of you last comment. Why is the name of the physical file you store on the server side relevant for its later delivery? You have to remember the chosen name anyway. – arkascha Apr 21 '17 at 09:42
  • I am just a beginner, can anyone correct my php? please! – HappyChan Apr 21 '17 at 10:06

0 Answers0