0

I want to upload images to the server but how to rename them so they not be identical ?

in the server its saved image_upload.jpg in this php code I rename the file

  $base=$_POST['image'];
     $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
    $file = fopen('uploaded_images.jpg', 'wb'); // here I am naming it

I am thinking in java to add the time upload it in milliseconds ... like image_upload3123.jpg with this

  Calendar thisCal = Calendar.getInstance();
  thisCal.getTimeInMillis();

then send the value send it to server .. is this good way? or there is better way?

Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • Not everyone may have JAVA enabled on their end (unless you meant Javascript; those are two different animals altogether, which still applies). Best to use a PHP/serverside method and there are plenty of examples out there. – Funk Forty Niner Sep 12 '15 at 14:25
  • @Fred-ii- what you mean 'Not everyone may have JAVA enabled on their end.` can you explain please. and please can you point out for an example ? – Moudiz Sep 12 '15 at 14:26
  • I made an edit to my comment; reload it. – Funk Forty Niner Sep 12 '15 at 14:26
  • fastest solution is to add the current time to the file name with time() command – Daniel Mendel Sep 12 '15 at 14:27
  • $file = fopen('uploaded_images'.time().'.jpg', 'wb'); // here I am naming it – Daniel Mendel Sep 12 '15 at 14:28
  • @Fred-ii- I meant java as for android. – Moudiz Sep 12 '15 at 14:28
  • Add current Time stamp as daniel said. If you want identify each image use any unique id and associate it with timestamp – I-Kod Sep 12 '15 at 14:29
  • @DanielMendel time() is in seconds ? can I make it in milliseconds ? I am afraid to have identical naming – Moudiz Sep 12 '15 at 14:29
  • then that would only be good of people using Android to upload with, and/or Java enabled on their system. I for one, don't have Java enabled on my PC, just saying. Again, use a serverside method. – Funk Forty Niner Sep 12 '15 at 14:30
  • @Fred-ii- Ill go for server side – Moudiz Sep 12 '15 at 14:31
  • @I-Kod how to set a unique id ? is there a fuction in php for that ? – Moudiz Sep 12 '15 at 14:31
  • @Moudiz, The correct solution is to always check if the file doesn't exist before, since if you are going to name something randomly there is always a slight chance of it already existing , you can use various encrpytions such as md5 to generate a unique string but you will need some unqie ids to begin with – Daniel Mendel Sep 12 '15 at 14:32
  • I found a duplicate question for exactly what you need. See the accepted answer for it. @Moudiz – Funk Forty Niner Sep 12 '15 at 14:32
  • @DanielMendel md5 will always have the same hash for the same file name. – Funk Forty Niner Sep 12 '15 at 14:33
  • @Fred -ii, you are correct that's why I added the requirement of having a unique id to begin with – Daniel Mendel Sep 12 '15 at 14:34
  • Yes there is function called uniqid() @Moudiz – I-Kod Sep 12 '15 at 14:34
  • @DanielMendel That was just a sidenote about MD5. But what you said about using unique id's would be the method to use for sure. *Cheers* – Funk Forty Niner Sep 12 '15 at 14:35
  • @DanielMendel Intersting ... please can you tell me how to do a check if image exists ? a simple example would be great ... Fred -ii thanks ill check the asnwer – Moudiz Sep 12 '15 at 14:35
  • You're welcome @Moudiz *Cheers* – Funk Forty Niner Sep 12 '15 at 14:36
  • To hit two posts with one stone, http://stackoverflow.com/questions/10015232/php-check-file-name-exist-rename-the-file which would also be another duplicate. The duplicate it was closed with, was based on your original post. Therefore a reopen would only apply if you edited your question with your "new" requirement that was suggested by someone other than yourself. @Moudiz You could have Google'd this. – Funk Forty Niner Sep 12 '15 at 14:37
  • @I-Kod, nice I didn't know it exists, makes it much more easy, Moudiz that is what you will want to use, Also based on the documentation it is suggested to md5 the uniqid value to get a more random file name to prevent hacking – Daniel Mendel Sep 12 '15 at 14:38
  • your really helped me guys thank you `Ikod` `Fred` and `Daniel` ill check all the info you have provided – Moudiz Sep 12 '15 at 14:40
  • 1
    here is a new requirement suggested by someone else. Delete the question – Drew Sep 12 '15 at 14:41
  • @Fred-ii- I did try [google](http://www.google.com.lb/search?hl=en-LB&source=hp&biw=&bih=&q=android+rename+a+photo+after+uploading&gbv=2&oq=android+rename+a+photo+after+uploading&gs_l=heirloom-hp.3..0l10.270295.278455.0.278671.38.21.0.0.0.0.1956.3505.2j0j2j0j2j8-1.7.0....0...1ac.1.34.heirloom-hp..34.4.2717.KiPbtkeEhBk) ... Drew still need 4 to close , however comments contain useful info for people like me who new to php – Moudiz Sep 12 '15 at 14:49
  • 1
    @Moudiz Try out something from the links that I have provided to you. If you have a hard time with something, edit your question with your new code. The question could then be voted to reopen. However, a fair amount of effort on your part is required. You will succeed ;-) Just put your mind to it. Use error reporting also to check for errors. – Funk Forty Niner Sep 12 '15 at 14:51

0 Answers0