0

I used this code for uploading an image in my ionic app. Everything went well, except the filename.

The file name become ".Pic.jpg"

Why is it happening and what should I do to overcome this problem? Specifically, how can I make the filename of the uploaded image have the same name as the selected image?

Community
  • 1
  • 1

1 Answers1

0

I'm checked code which you are referred. In that uploaded file is there, but file name is missing. For that you need to mention the name of the image to upload file. For example

       var options = {
          quality: 90,
          destinationType: Camera.DestinationType.FILE_URI,
          sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
          allowEdit: true,
          encodingType: Camera.EncodingType.JPEG,
          popoverOptions: CameraPopoverOptions,
          saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {  
          $scope.profileImageData = imageData;  

          $scope.profileImageName = Math.floor(Math.random()*16777215).toString(16)

          var server = baseUrlImageUpload+'upload.php';
          filePath = imageData;
          var date = new Date();

          var options = {
            fileKey: "file",
            fileName: $scope.profileImageName,
            chunkedMode: false,
            mimeType: "image/jpg"
          };

          $cordovaFileTransfer.upload(server, filePath, options).then(function(result) {
            $ionicLoading.hide();
            //alert(JSON.stringify(result.response));

          }, function(err) {
           $ionicLoading.hide(); 
           //alert("Select image again") 

            var alertPopup = $ionicPopup.alert({
              title: 'Sorry!',
              template: 'Please select image again.'
            });

          }, function (progress) {
             $ionicLoading.show({
               template: 'Updating Image...'
            }); 
          });

        });

In above code i'm passing image name randomly in $scope.profileImageName = Math.floor(Math.random()*16777215).toString(16).

Your php code like this

<?php

  if ($_FILES["file"]["error"] > 0) {
    echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
  }  else  {

  if ($_FILES["file"]["size"] < 1024 * 1024) {
    if (file_exists("/files/".$_FILES["file"]["name"]))
    {
      echo $_FILES["file"]["name"] . " already exists. No joke-- this error is almost <i><b>impossible</b></i> to get. Try again, I bet 1 million dollars it won't ever happen again.";
    }  else  {
      $number = rand(111111,999999);
    move_uploaded_file($_FILES["file"]["tmp_name"], 'images/' .$_FILES["file"]["name"].'.jpg');
    echo "Done";
    //return $number;
    }
  }  else {
     // echo "File Size: " . ($_FILES["file"]["size"] / 1024 * 1024) . " MB<br />";
    echo "Please upload below 1MB images";
  }
}

?>

I hope this will help you :-)

Sam
  • 149
  • 4
  • 15