How to Make Multiple Uploads of Images Using PHP or JavaScript

The PHP and JavaScript languages work together to prompt users to upload multiple files to your Web server. JavaScript prompts the user for each file and PHP handles the request to upload the file after the user clicks the "Submit" button. You use this feature when you want users to upload multiple images or documents to host them on your Web space.

Instructions

    • 1

      Right-click the PHP page you want to use to upload the files. Click "Open With" and select your preferred PHP editor.

    • 2

      Add the JavaScript function. The JavaScript function continues to show a new "Upload" button after the user submits the first image. It also handles the animation that displays when as the images upload. Copy and paste the following function to the page:

      function stopUpload(msg,file){
      if (success == 1){
      result = '<span>The file was uploaded successfully!</span><br/><br/>';
      addImage("upload.php?id=" + file);
      }
      else {
      result = '<span>There was an error during file upload!' + msg+ '</span>
      }
      document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" onchange="startUpload();document.form.submit();" /><\/label>';
      return true;

      }

    • 3

      Add the PHP script that uploads the image to the server. The following code loops through each image and uploads the image to the server:

      for (int i = 0; i++ i < imagecount) {
      $width = $img.width;
      $height = $img.height;
      $uploaddir = getcwd(); //
      $path= $uploaddir.DIRECTORY_SEPARATOR.$file;
      @move_uploaded_file($file, $path)

      }

    • 4

      Save the file and open it in your browser to test the new upload function. Upload one image, and then upload multiple images to verify that the functions work.

Related Searches:

References

Comments

Related Ads

Featured